对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