我正試圖用Python解壓縮文件。我使用下面的函數:ZipFile不能提取目錄中的壓縮文件
def unzip(source_filename, dest_dir):
zf = zipfile.ZipFile(source_filename)
for member in zf.infolist():
# Path traversal defense copied from
# http://hg.python.org/cpython/file/tip/Lib/http/server.py#l789
words = filter(None, member.filename.split('/'))
path = dest_dir
for word in words[:-1]:
drive, word = os.path.splitdrive(word)
head, word = os.path.split(word)
if word in (os.curdir, os.pardir): continue
path = os.path.join(path, word)
zf.extract(member, path)
當解壓與目錄的.zip稱爲「鬼」,我得到以下錯誤:
Traceback (most recent call last):
File "MCManager.py", line 137, in add
unzip(addedFilepath, dirUnzipped)
File "MCManager.py", line 19, in unzip
zf.extract(member, path)
File "/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/zipfile.py", line 928, in extract
File "/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/zipfile.py", line 962, in _extract_member
File "/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/os.py", line 157, in makedirs
OSError: [Errno 20] Not a directory: '/Users/student/Library/Application Support/minecraft/temp/unzipped/gui/gui'
這是一個問題的ZipFile()?
是否存在'/ Users/student/Library/Application Support/minecraft/temp/unzipped/gui'?如果是,是'/ Users/student/Library/Application Support/minecraft/temp/unzipped/gui/gui'目錄嗎? – nneonneo
@nneonneo事實證明,當我用普通的zip實用工具解壓縮它時,「gui」是一個帶有.png的目錄,但是我的腳本創建了一個帶有該名稱的空文件。 – tkbx
是的,似乎任何時候zip包含一個目錄,我得到這個錯誤。 – tkbx