Python zipfile crashes gives an error for some files -
i have simple code zip files using zipfile
module. able zip files filenotfound
error others. have checked if file size error not.
i can pack files name example file.py
when have file inside directory 'analyze files en-us_es-es.xlsx' if fails.
it works when change os.path.basename
os.path.join
i don't want zip whole folder structure, want have flat structure in zip.
here code:
import os import zipfile path = input() x=zipfile.zipfile('new.zip', 'w') root, dir, files in os.walk(path): eachfile in files: x.write(os.path.basename(eachfile)) x.close()
error looks 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] system cannot find file specified: 'analyze files en-us_ar-sa.xlsx'*
thanks help.
simply change working directory add file without original directory structure.
import os import zipfile path = input() basedir = os.getcwd() zipfile.zipfile('new.zip', 'w') z: root, dir, files in os.walk(path): os.chdir(root) eachfile in files: z.write(eachfile) os.chdir(basedir)
Comments
Post a Comment