I am trying a straight forward code:
from zipfile import ZipFile
password = '1sS34nConn3ryTh3B3st007?'
zip_file = 'file.zip'
with ZipFile(zip_file) as zf:
zf.extractall(pwd=bytes(password,'utf-8'))
Traceback (most recent call last):
File "<stdin>", line 2, in <module>
File "/usr/lib64/python3.6/zipfile.py", line 1524, in extractall
self._extract_member(zipinfo, path, pwd)
File "/usr/lib64/python3.6/zipfile.py", line 1577, in _extract_member
with self.open(member, pwd=pwd) as source, \
File "/usr/lib64/python3.6/zipfile.py", line 1446, in open
raise RuntimeError("Bad password for file %r" % name)
RuntimeError: Bad password for file <ZipInfo filename='file.csv' compress_type=99 file_size=272074 compress_size=60230>
It works perfectly fine when I am extracting it on windows using either 7z or winrar. py7zr also gives error.
I've tried to encrypt a file from 7z Ui on Windows with your password.
After I ran this script:
from zipfile import ZipFile
password = '1sS34nConn3ryTh3B3st007?'
zip_file = 'file.zip'
zf = ZipFile(self.archive_name, 'r')
zf.setpassword(bytes(password,"utf-8"))
zf.extractall(path=".")
No issue. Can you try as well?
You need to use pyzipper module instead of zipfile, because of the newer AES-256 encoding.
Related
import pandas as pd
data = pd.read_excel (r'C:\Users\royli\Downloads\Product List.xlsx',sheet_name='Sheet1' )
df = pd.DataFrame(data, columns= ['Product'])
print (df)
Error Message
Traceback (most recent call last):
File "main.py", line 3, in <module>
Traceback (most recent call last):
File "main.py", line 3, in <module>
data = pd.read_excel (r'C:\Users\royli\Downloads\Product List.xlsx',sheet_name='Sheet1' )
File "/opt/virtualenvs/python3/lib/python3.8/site-packages/pandas/util/_decorators.py", line 296, in wrapper
return func(*args, **kwargs)
File "/opt/virtualenvs/python3/lib/python3.8/site-packages/pandas/io/excel/_base.py", line 304, in read_excel
io = ExcelFile(io, engine=engine)
File "/opt/virtualenvs/python3/lib/python3.8/site-packages/pandas/io/excel/_base.py", line 867, in __init__
self._reader = self._engines[engine](self._io)
File "/opt/virtualenvs/python3/lib/python3.8/site-packages/pandas/io/excel/_xlrd.py", line 22, in __init__
super().__init__(filepath_or_buffer)
File "/opt/virtualenvs/python3/lib/python3.8/site-packages/pandas/io/excel/_base.py", line 353, in __init__
self.book = self.load_workbook(filepath_or_buffer)
File "/opt/virtualenvs/python3/lib/python3.8/site-packages/pandas/io/excel/_xlrd.py", line 37, in load_workbook
return open_workbook(filepath_or_buffer)
File "/opt/virtualenvs/python3/lib/python3.8/site-packages/xlrd/__init__.py", line 111, in open_workbook
with open(filename, "rb") as f:
FileNotFoundError: [Errno 2] No such file or directory: 'C:\\Users\\royli\\Downloads\\Product List.xlsx'
KeyboardInterrupt
Generally when I get that problem am gonna change \ symbols to \ \ symbols and generally its solved. Try it.
I had this problem in Visual Studio Code.
table = pd.read_excel('Sales.xlsx')
When running the program on Pycharm, there were no errors.
When trying to run the same program in Visual Studio Code, it showed an error, without any changes.
To fix it, I had to address the file with //. Ex:
table = pd.read_excel('C:\\Users\\paste\\Desktop\\archives\\Sales.xlsx')
I am using Pycharm and after reviewing the Post and replies, I was able to get this resolved (thanks very much). I didn't need to specify a worksheet, as there is only one sheet on the Excel file I am reading.
I had to add the r (raw string), and I also removed the drive specification c:
data = pd.read_excel(r'\folder\subfolder\filename.xlsx')
The problem occurs where the string variable tries to get read by zipfile.Zipfile(variable). The callback is file does not exist. This is because it is adding an extra \ to each folder. I tried several things to try and make the string into a literal but when I do this it prints an extra \\\. I tried even making that a literal but as I would expect it repeated the same thing returning \\\\\.Any insight would be greatly appreciated!!!
The code structure is below and here are the callback functions
Traceback (most recent call last):
File "C:\Users\Yume\Desktop\Walk a Dir Unzip and copy to.py", line 17, in <module>
z = zipfile.ZipFile(zf, 'r')
File "C:\Users\Yume\AppData\Local\Programs\Python\Python36-32\lib\zipfile.py", line 1090, in __init__
self.fp = io.open(file, filemode)
FileNotFoundError: [Errno 2] No such file or directory: "C:\\Users\\Yume\\AppData\\Roaming\\
Traceback (most recent call last):
File "C:\Users\Yume\Desktop\Walk a Dir Unzip and copy to.py", line 17, in <module> z = zipfile.ZipFile(rzf, 'r')
File "C:\Users\Yume\AppData\Local\Programs\Python\Python36-32\lib\zipfile.py", line 1090, in __init__
self.fp = io.open(file, filemode)
OSError: [Errno 22] Invalid argument: '"C:\\\\Users\\\\Yume\\\\AppData\\\\Roaming\\\\
z = zipfile.ZipFile(rrzf, 'r')
File "C:\Users\Yume\AppData\Local\Programs\Python\Python36-32\lib\zipfile.py", line 1090, in __init__
self.fp = io.open(file, filemode)
OSError: [Errno 22] Invalid argument: '\'"C:\\\\\\\\Users\\\\\\\\Yume\\\\\\\\AppData\\\\\\\\Roaming\\\\\\\
CODE:
import os
import re
from shutil import copyfile
import zipfile
import rarfile
isRar = re.compile('.+\\.rar')
isZip = re.compile('.+\\.zip')
for folderName, subfolders, filenames in os.walk(r'C:\Users\Yume\AppData'):
if isZip.match(str(filenames)):
zf = folderName+ str(subfolders)+str(filenames)
rzf = '%r'%zf
z = zipfile.ZipFile(zf)
z.extractall(r'C:\Users\Yume')
z.close()
if isRar.match(str(filenames)):
rf = folderName+ str(subfolders)+str(filenames)
rrf = "%r"%rf
r = rarfile.RarFile(rf)
r.extractall(r'C:\Users\Yume')
r.close()
My problem was corrected by running a for loop through the file list created by os walk. I cleaned up my code and here is a working program that walks a directory find a compressed .zip or .rar and extracts it to the specified destination.
import os
import zipfile
import rarfile
import unrar
isRar = re.compile('.*\\.rar')
isZip = re.compile('.*\\.zip')
for dirpath, dirnames, filenames in os.walk(r'C:\Users\'):
for file in filenames:
if isZip.match(file):
zf = os.path.join(dirpath, file)
z= zipfile.ZipFile(zf)
z.extractall(r'C:\Users\Unzipped')
z.close()
if isRar.match(file):
rf = os.path.join(dirpath, file)
r = rarfile.RarFile(rf)
r.extractall(r'C:\Users\Unzipped')
r.close()
When I run my program, I get the following error and am not sure on how to correct it. Can someone help with explaining what this error is and how to correct it? Newb here so details are appreciated. Thanks for your time in advance!
Code:
#!/usr/bin/python
import zipfile
from PySide import QtGui
import re
#Select file to extract
app = QtGui.QApplication([])
dialog = QtGui.QFileDialog()
dialog.setFileMode(QtGui.QFileDialog.AnyFile)
if (dialog.exec()):
fileName = dialog.selectedFiles()
#Select Directory to extract to
dialog = QtGui.QFileDialog()
dialog.setFileMode(QtGui.QFileDialog.Directory)
dialog.setOption(QtGui.QFileDialog.ShowDirsOnly)
if (dialog.exec()):
dirName = dialog.selectedFiles()
print("Extracting.....")
zFile= zipfile.ZipFile(fileName)
zFile.extractall(dirName)
Error output:
Traceback (most recent call last):
File "C:\Users\Jennifer\Documents\BatchScripts\unzip.py", line 22, in <module>
zFile= zipfile.ZipFile(fileName)
File "C:\Python33\lib\zipfile.py", line 933, in __init__
self._RealGetContents()
File "C:\Python33\lib\zipfile.py", line 970, in _RealGetContents
endrec = _EndRecData(fp)
File "C:\Python33\lib\zipfile.py", line 237, in _EndRecData
fpin.seek(0, 2)
AttributeError: 'list' object has no attribute 'seek'
In your file and target directory code blocks, dialog.selectedFiles() returns a list. zipfile.ZipFile can only handle one file at a time, hence your error. To iterate over the list being provided by dialog.selectedFiles(), use the following:
for archive in fileName: # you should probably change it to fileNames to reflect its true nature
zfile = zipfile.ZipFile(archive)
print("Extracting " + str(zfile.filename) + "...")
zfile.extractall(dirName[0]) # also a list, extract to first item and ignore rest
and you should be all set.
from zipfile import ZipFile
fzip=ZipFile("crackme.zip")
fzip.extractall(pwd=b"mysecretpassword")
the script works only on IDLE, but when i run it from the command line, it displays:
unzip.py
fzip.extractall(pwd=b"mysecretpassword")
^
SyntaxError: invalid syntax
what's wrong?
It works (Ubuntu 13.04):
>>> import sys
>>> sys.version
'3.3.1 (default, Apr 17 2013, 22:32:14) \n[GCC 4.7.3]'
>>> from zipfile import ZipFile
>>> f = ZipFile('a.zip')
BTW, pwd should be bytes objects:
>>> f.extractall(pwd="mysecretpassword")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python3.3/zipfile.py", line 1225, in extractall
self.extract(zipinfo, path, pwd)
File "/usr/lib/python3.3/zipfile.py", line 1213, in extract
return self._extract_member(member, path, pwd)
File "/usr/lib/python3.3/zipfile.py", line 1275, in _extract_member
with self.open(member, pwd=pwd) as source, \
File "/usr/lib/python3.3/zipfile.py", line 1114, in open
raise TypeError("pwd: expected bytes, got %s" % type(pwd))
TypeError: pwd: expected bytes, got <class 'str'>
>>> f.extractall(pwd=b'mysecretpassword')
>>>
According to zipfile.ZipFile.extractall documentation:
Warning Never extract archives from untrusted sources without prior inspection. It is possible that files are created outside of
path, e.g. members that have absolute filenames starting with "/" or
filenames with two dots "..".
Changed in version 3.3.1: The zipfile module attempts to prevent that. See extract() note.
when I try the following program :
import wave
w = wave.open('f.wav', 'r')
for i in range():
frame = w.readframes(i)
the following error comes :
Traceback (most recent call last):
File "F:/Python31/fg.py", line 2, in <module>
w = wave.open('f.wav', 'r')
File "F:\Python31\lib\wave.py", line 498, in open
return Wave_read(f)
File "F:\Python31\lib\wave.py", line 159, in __init__
f = builtins.open(f, 'rb')
IOError: [Errno 2] No such file or directory: 'f.wav'
can u tell me wat could b the reason ???
The file is not in the path you put that Python interpreter can find. Check that f.wav is in the same path of your script (or chance the path in open).
Is not a wave issue at all.
You are running the python script from a directory where no file f.wav exists. It can't find the file to read. Either copy f.wav to that directory or run you script from the directory f.wav is located in.