![](/img/trans.png)
[英]Extract zip file and nested zip files into target directory using Python
[英]i need help to extract files from a zip file using python
我创建了一个名为shoppy的zip文件,并在其中放入了“ cats.txt”,现在我想提取它,但是我的代码无法正常工作,因此出现了此错误
AttributeError: '_io.TextIOWrapper' object has no attribute 'extract'
这是我的代码
from zipfile import *
z=open("shoppy.zip","U")
z.extract("cats.txt")
第一个问题是open()
是指内置函数,而不是zipfile
任何函数-没有zipfile.open()
函数。
要打开一个zip文件,请使用zipfile.ZipFile
类:
import zipfile
z = zipfile.ZipFile('shoppy.zip')
z.extract('cats.txt')
这会将文件解压缩到当前目录。 如果您想解压缩为字符串,可以使用zipfile.read()
:
content = z.read('cats.txt')
现在, content
将包含文件的解压缩内容。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.