【问题标题】:i need help to extract files from a zip file using python我需要帮助来使用 python 从 zip 文件中提取文件
【发布时间】:2016-06-27 23:37:46
【问题描述】:

我创建了一个名为 shoppy 的 zip 文件并将“cats.txt”放入其中,现在我想提取它,但我的代码不起作用,它给了我这个错误

AttributeError: '_io.TextIOWrapper' object has no attribute 'extract' 

这是我的代码

from zipfile import *

z=open("shoppy.zip","U")
z.extract("cats.txt")

【问题讨论】:

标签: python zip extract


【解决方案1】:

第一个问题是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 将包含文件的解压缩内容。

【讨论】:

    猜你喜欢
    • 2020-11-30
    • 1970-01-01
    • 2016-07-05
    • 1970-01-01
    • 1970-01-01
    • 2020-03-28
    • 1970-01-01
    • 2023-04-07
    • 1970-01-01
    相关资源
    最近更新 更多