![](/img/trans.png)
[英]How can i extract files using custom names with zipfile module from python?
[英]How can I use non ASCII file names in a zipped file and extract them using ZipFile in Python 2.4?
我在名为öl_och_ål_är_gott.zip的zip归档文件中有一个öl_och_ål_är_gott.txt文件。 存档不是使用zipfile创建的。 它可以来自能够创建zip存档的任何软件。
src = open(file_path, "rb" )
zip_file = ZipFile(src)
for info in zip_file.infolist():
print info.filename
...
打印出:
”l_och_†l_„r_gott.txt
如何强制zipfile代表我要表示的名称?
docs指出,ZIP文件没有正式的文件名编码。 如果您有Unicode文件名(如您的情况),则必须在传递它们之前以所需的编码将它们转换为字节字符串。
虽然我不知道为什么它对您不起作用。
>>> src = open('/Desktop/test.zip', 'rb')
>>> zip_file = zipfile.ZipFile(src)
>>> for info in zip_file.infolist():
... print info.filename
...
öl_och_ål_är_gott
在我的Ubuntu盒子上。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.