我正在尝试使用以下代码解压缩根文件夹中的所有zip文件夹;此代码在此主题中找到:
Unzip zip files in folders and subfolders with python
rootPath = u"//rootdir/myfolder" # CHOOSE ROOT FOLDER HERE
pattern = '*.zip'
for root, dirs, files in os.walk(rootPath):
for filename in fnmatch.filter(files, pattern):
print(os.path.join(root, filename))
zipfile.ZipFile(os.path.join(root, filename)).extractall(os.path.join(root, os.path.splitext(filename)[0]))
但是我一直收到这个错误,说FileNotFoundError说xlsx文件不存在:
Traceback (most recent call last):
File "//rootdir/myfolder/Python code/unzip_helper.py", line 29, in <module>
zipfile.ZipFile(os.path.join(root, filename)).extractall(os.path.join(root, os.path.splitext(filename)[0]))
File "//rootdir/myfolder/Python\Python36-32\lib\zipfile.py", line 1491, in extractall
self.extract(zipinfo, path, pwd)
File "//myaccount/Local\Programs\Python\Python36-32\lib\zipfile.py", line 1479, in extract
return self._extract_member(member, path, pwd)
File "//myaccount/Local\Programs\Python\Python36-32\lib\zipfile.py", line 1542, in _extract_member
open(targetpath, "wb") as target:
FileNotFoundError: [Errno 2] No such file or directory: '\\rootdir\myfolder\._SGS Naked 3 01 WS Kappa Coated and a very long very long file name could this be a problem i dont think so.xlsx'
我的问题是,为什么要解压这个excel文件呢?!
我怎样摆脱错误?
我也尝试过使用r而不是u来表示rootPath:
rootPath = r"//rootdir/myfolder"
我得到同样的错误。
真的很感激任何帮助!
答案 0 :(得分:1)
某些文件名和目录名可能在其名称中包含额外的点,因此最后一行与Windows文件名不同,在Unix上可以有点:
zipfile.ZipFile(os.path.join(root, filename)).extractall(os.path.join(root, os.path.splitext(filename)[0]))
此行失败。要了解这种情况如何发生:
>>> filename = "my.arch.zip"
>>> root = "/my/path/to/mydir/"
>>> os.path.join(root, os.path.splitext(filename)[0])
'/my/path/to/mydir/my.arch'
无论是否有额外的点,代码中仍会出现问题:
>>> os.path.join(root, os.path.splitext(filename)[0])
'/my/path.to/mydir/arch'
如果找不到'/my/path.to/mydir/arch'
,则会引发FileNotFoundError
。我建议你在路径中明确,否则你必须确保这些目录的存在。
ZipFile.extractall(path=None, members=None, pwd=None)
将存档中的所有成员解压缩到当前工作目录。
path
指定要提取到的其他目录...
除非path
是现有目录,否则将引发FileNotFoundError
。