windows-將zip文件內容提取到Python 2.7中的特定目錄
這是我當前用于提取與腳本位于同一當前工作目錄中的zip文件的代碼。 如何指定要提取到的其他目錄?
我嘗試的代碼未將其提取到我想要的位置。
import zipfile
fh = open('test.zip', 'rb')
微軟python教程,z = zipfile.ZipFile(fh)
for name in z.namelist():
outfile = open(name, 'wb')
outfile.write('C:\\'+z.read(name))
outfile.close()
fh.close()
winpython,6個解決方案
105 votes
我認為您在這里只是一頭霧水。 可能應該是以下內容:
import zipfile
fh = open('test.zip', 'rb')
z = zipfile.ZipFile(fh)
python map、for name in z.namelist():
outpath = "C:\\"
z.extract(name, outpath)
fh.close()
如果只想提取所有文件:
import zipfile
python os,with zipfile.ZipFile('test.zip', "r") as z:
z.extractall("C:\\")
對最新版本的Python使用pip install zipfile36
import zipfile36
secretmike answered 2020-01-17T17:38:59Z
12 votes
python ide。我嘗試了該線程中的其他答案,但最終的解決方案很簡單:
zfile = zipfile.ZipFile('filename.zip')
zfile.extractall(optional_target_folder)
查看extractall,但僅將其與可信賴的zip文件一起使用。
fiatjaf answered 2020-01-17T17:39:23Z
5 votes
python編程?添加到上述secretmike的答案中,支持python 2.6提取所有文件。
import zipfile
import contextlib
with contextlib.closing(zipfile.ZipFile('test.zip', "r")) as z:
z.extractall("C:\\")
Slakker answered 2020-01-17T17:39:43Z
python tuple,3 votes
如果您只想使用Python從命令行中提取一個zip文件(例如,因為您沒有可用的unzip命令),則可以直接調用zipfile模塊
python -m zipfile -e monty.zip target-dir/
看一下文檔。 它還支持壓縮和列出內容。
Peter Gibson answered 2020-01-17T17:40:08Z
2 votes
qpython,彼得·德·里瓦茲(Peter de Rivaz)在上面的評論中有觀點。 您將要在對open()的調用中包含目錄。您將要執行以下操作:
import zipfile
import os
os.mkdir('outdir')
fh = open('test.zip','rb')
z = zipfile.ZipFile(fh)
python3。for name in z.namelist():
outfile = open('outdir'+'/'+name, 'wb')
outfile.write()
outfile.close()
fh.close()
razzmataz answered 2020-01-17T17:40:29Z
python中的zip函數、0 votes
我已經修改了代碼,要求用戶輸入文件名及其需要提取的位置,因此用戶將對放置提取的文件夾的位置以及應該為提取的文件夾分配什么名稱有更多的控制權。
import zipfile
#picking zip file from the directory
ZipFileName = raw_input("Enter full path to zip file:")
fh = open( ZipFileName , 'rb')
python解壓zip文件到指定目錄?z = zipfile.ZipFile(fh)
#assigning a name to the extracted zip folder
DestZipFolderName = raw_input("Assign destination folder a name: ")
DestPathName = raw_input("Enter destination directory: ")
DestPath = DestPathName + "\\" + DestZipFolderName
for name in z.namelist():
python zip函數的用法。outpath = DestPath
z.extract(name, outpath)
fh.close()
sarfarazit08 answered 2020-01-17T17:40:49Z
版权声明:本站所有资料均为网友推荐收集整理而来,仅供学习和研究交流使用。
工作时间:8:00-18:00
客服电话
电子邮件
admin@qq.com
扫码二维码
获取最新动态