zipfile 中的一些坑

  1. ZipFile.setpassword() 网上有些博客说是设置压缩包密码,是错误的,应该是设置默认解压缩密码。对于设置压缩文件密码可以使用pyminizip
  2.  windows下ZipFile.extract('test\\1.txt') 会报错,原因是在zipfile中存储的名字为test/1.txt,会找不到该文件。
  3. windows平台下出现以下错误
File name in directory 'test/1/test.txt' and header b'test\\1\\test.txt' differ


可能的原因为

  • 在ZipFile.write()之后,直接ZipFile.extract()。解决方法为先ZipFile.close() 然后再zipfile.ZipFile()。
  • zipfile创建zip文件时,使用了zipfile.ZIP_DEFLATED压缩格式

   4. 删除压缩包内文件时,zipfile没有提供相关方法。解决方法为新建一个zip,然后将**不删除**的文件写入新zip,然后删除原来的zip并重命名新zip。

猜你喜欢

转载自blog.csdn.net/Magic1an/article/details/81951907