Jan-30-2019, 08:31 AM
in below program, i try to zip the files under 1139 directory.
Traceback (most recent call last):
File "C:\dell\test_zip.py", line 13, in <module>
print("\narchiving %s..."%(zip_f))
NameError: name 'zip_f' is not defined
my zip_f is defined with os.path.basename(folder)+'_'+str(ver)+'.zip', why still error? thanks
import os import datetime import zipfile def zipt(folder): folder = os.path.abspath(folder) ver = 1 while True: zip_f = os.path.basename(folder)+'_'+str(ver)+'.zip' if not os.path.exsits(zip_f): break ver += 1 print("\narchiving %s..."%(zip_f)) zip_file=zipfile.ZipFile(zip_f,'w') for foldername,subfolders,filenames in os.walk(folder): print("\nadding files in the %s..."%(foldername)) zip_file.write(foldername) for filename in filenames: new_base=os.path.basename(folder)+'_' if filename.startswith(new_base) and filename.endswith('.zip'): continue zip_file.write(os.path.join(foldername,filename)) zip_file.close() print("\narchiving complete, {} created successfully".format(zip_f)) zipt('c:\\dell\\1139')but it runs with error :
Traceback (most recent call last):
File "C:\dell\test_zip.py", line 13, in <module>
print("\narchiving %s..."%(zip_f))
NameError: name 'zip_f' is not defined
my zip_f is defined with os.path.basename(folder)+'_'+str(ver)+'.zip', why still error? thanks