解压文件夹和子文件夹中的 zip 文件
Unzip zip files in folders and subfolders
我尝试解压 150 个 zip 文件。所有 zip 文件的名称都不同,它们都分布在一个大文件夹中,该文件夹分为许多子文件夹和子子 folders.i 想要将每个存档解压缩到与原始 zip 文件名相同的单独文件夹并且也与原始 zip 文件位于同一位置。我的代码是:
import zipfile
import os,os.path,sys
pattern = '*.zip'
folder = r"C:\Project\layers"
files_process = []
for root,dirs,files in os.walk(r"C:\Project\layers"):
for filenames in files:
if filenames == pattern:
files_process.append(os.path.join(root, filenames))
zip.extract()
在我 运行 代码之后没有任何反应。
在此先感谢您对此的任何帮助。
您可以使用 Path.rglob()
to enumerate zip-files recursively and shutil.unpack_archive()
解压缩 zip 文件:
#!/usr/bin/env python3
import logging
from pathlib import Path
from shutil import unpack_archive
zip_files = Path(r"C:\Project\layers").rglob("*.zip")
while True:
try:
path = next(zip_files)
except StopIteration:
break # no more files
except PermissionError:
logging.exception("permission error")
else:
extract_dir = path.with_name(path.stem)
unpack_archive(str(path), str(extract_dir), 'zip')
它 "extract[s] each archive to separate folder with the same name as the original zip file name and also in the same place as the original zip file" 例如,它将 'layers/dir/file.zip'
存档提取到 'layers/dir/file'
目录。
更新:
最后,这段代码对我有用:
import zipfile,fnmatch,os
rootPath = r"C:\Project"
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]))
将所有文件解压到一个临时文件夹中(Ubuntu)
import tempfile
import zipfile
tmpdirname = tempfile.mkdtemp()
zf = zipfile.ZipFile('/path/to/zipfile.zip')
for fn in zf.namelist():
temp_file = tmpdirname+"/"+fn
#print(temp_file)
f = open(temp_file, 'w')
f.write(zf.read(fn).decode('utf-8'))
f.close()
我尝试解压 150 个 zip 文件。所有 zip 文件的名称都不同,它们都分布在一个大文件夹中,该文件夹分为许多子文件夹和子子 folders.i 想要将每个存档解压缩到与原始 zip 文件名相同的单独文件夹并且也与原始 zip 文件位于同一位置。我的代码是:
import zipfile
import os,os.path,sys
pattern = '*.zip'
folder = r"C:\Project\layers"
files_process = []
for root,dirs,files in os.walk(r"C:\Project\layers"):
for filenames in files:
if filenames == pattern:
files_process.append(os.path.join(root, filenames))
zip.extract()
在我 运行 代码之后没有任何反应。 在此先感谢您对此的任何帮助。
您可以使用 Path.rglob()
to enumerate zip-files recursively and shutil.unpack_archive()
解压缩 zip 文件:
#!/usr/bin/env python3
import logging
from pathlib import Path
from shutil import unpack_archive
zip_files = Path(r"C:\Project\layers").rglob("*.zip")
while True:
try:
path = next(zip_files)
except StopIteration:
break # no more files
except PermissionError:
logging.exception("permission error")
else:
extract_dir = path.with_name(path.stem)
unpack_archive(str(path), str(extract_dir), 'zip')
它 "extract[s] each archive to separate folder with the same name as the original zip file name and also in the same place as the original zip file" 例如,它将 'layers/dir/file.zip'
存档提取到 'layers/dir/file'
目录。
更新:
最后,这段代码对我有用:
import zipfile,fnmatch,os
rootPath = r"C:\Project"
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]))
将所有文件解压到一个临时文件夹中(Ubuntu)
import tempfile
import zipfile
tmpdirname = tempfile.mkdtemp()
zf = zipfile.ZipFile('/path/to/zipfile.zip')
for fn in zf.namelist():
temp_file = tmpdirname+"/"+fn
#print(temp_file)
f = open(temp_file, 'w')
f.write(zf.read(fn).decode('utf-8'))
f.close()