I have a simple code to zip files using zipfile
module. I am able to zip some files but I get FileNotFound
error for the others. I have checked if this is file size error but its not.
I can pack files with name like example file.py
but when I have a file inside a directory like 'Analyze Files en-US_es-ES.xlsx' if fails.
It works when I change os.path.basename
to os.path.join
but I don't want to zip whole folder structure, I want to have flat structure in my zip.
Here is my code:
import os
import zipfile
path = input()
x=zipfile.ZipFile('new.zip', 'w')
for root, dir, files in os.walk(path):
for eachFile in files:
x.write(os.path.basename(eachFile))
x.close()
Error looks like this:
Traceback (most recent call last):
File "C:/Users/mypc/Desktop/Zip test.py", line 15, in <module>
x.write(os.path.basename(eachFile))
File "C:\Python34\lib\zipfile.py", line 1326, in write
st = os.stat(filename)
FileNotFoundError: [WinError 2] The system cannot find the file specified: 'Analyze Files en-US_ar-SA.xlsx'*
Thanks for any help.
path
? How isZipFile
to know whereAnalyze Files en-US_ar-SA.xlsx
is on your system? – Peter Wood Apr 29 '15 at 6:58