在Unix中使用Python Zipfile的摘录

2024-04-30 06:07:42 发布

您现在位置:Python中文网/ 问答频道 /正文

我正试图在名为“fname”的Python脚本中使用以下代码解压缩名为“fname”的zip文件解开拉链.py“通过使用python模块“zipfile”: `你知道吗

fh = open(fname, 'rb')
z = zipfile.ZipFile(fh)
for name in z.namelist():
    print("Extracting " + name)
    dirNameFile = dStr + "_" + name.split('.')[0]
    path = dirNameZip + "/" + dirNameFile + "/content"
    if os.path.isfile(path + name):
        os.remove(path + name)
    z.extract(name, path)
    if not os.path.isdir(path + "/streams"):
        os.mkdir(path + "/streams")
fh.close()
`

这在Windows中运行良好,但在Linux中输出如下:

Traceback (most recent call last):
  File "unzipper.py", line 31, in ?
    z.extract(name, path)
AttributeError: ZipFile instance has no attribute 'extract'

“zipfile”python模块应该是跨平台的,那么为什么还有区别呢? 多谢了


Tags: 模块pathnameinpyifosextract