對Python來說是新手,但對於編程來說很古老。我來到這裏是因爲無論何時我搜索Python參考,我的最佳答案都在這裏。當談到目前的問題時,請相信我,我已經廣泛地看過這裏,雖然我得到了我想要的答案,但它們並不總是有效。用Python編寫的壓縮文件
所以這是我的問題。非常簡單,我只是想用Python來壓縮文件,並驗證所述壓縮文件。
下面是相關代碼:
import zipfile
archive_file_list = ['archive1','archive2']
LogIt(log_file,"checking for existance of archive directory")
if os.path.isdir(archive_path) == True:
LogIt(log_file,'archive directory found')
LogIt(log_file,'looking for files to archive')
for a in archive_file_list:
target_file = archive_path + a + '.txt'
LogIt(log_file,'looking for file : ' + target_file)
if os.path.isfile(target_file) == True:
LogIt(log_file,'file found')
zip_file = archive_path + a + '.zip'
LogIt(log_file,'creating zip file : ' + zip_file)
zf = zipfile.ZipFile(zip_file,'w')
zf.write(target_file,zip_file,compress_type=zipfile.ZIP_DEFLATED)
zf.close
LogIt(log_file,'Zip file created')
LogIt(log_file,'verifying')
if os.path.isfile(zip_file) == True:
LogIt(log_file,'zip file exists')
LogIt(log_file,'verifying zip file')
if zipfile.is_zipfile(zip_file) == True:
LogIt(log_file,'file ' + zip_file + ' verified')
else:
LogIt(log_file,'file ' + zip_file + ' not verified')
# print_info(zip_file)
else:
LogIt(log_file,'unable to verify zip file')
else:
LogIt(log_file,'file not found')
else:
LogIt(log_file,'archive path not found, no files to zip')
對於它工作正常,在大多數情況下,指定的文件是壓縮。我可以通過pkzip或Rar在我移動的任何機器上讀取它。但是當我真的檢查這是否是一個有效的zip文件時,我關閉了,用一個像樣的編輯器看這個文件表明它確實以「PK」或「Pk」開始,我忘記了,但它肯定會出現有效。
if zipfile.is_zipfile(zip_file) == True:
這總是返回False,我只是不明白它。
在此先感謝您的幫助。 吉姆
對不起,代碼的剪切/粘貼很醜陋,相信我,當我說它在句法上是正確的。 – Trashman