Python;如何在不包含父目录的情况下提取文件
Python; how to extract files without including parent directory
在 Python 方面我是菜鸟,我需要帮助!我正在尝试将 zipfile
模块用于最新版本的 Python 3,但在准备提取 zip 文件时我无法尝试排除父目录(文件夹)。
zip 文件目录示例如下所示:
Content/
Content/Runtime/
Content/Runtime/test.txt
Content/Runtime/test.png
Content/Documentation/
Content/Documentation/license.txt
我不知道如何从提取中排除父目录(第一个文件夹)。我只想提取 Runtime & Documentation 目录及其内容。
请帮忙,谢谢,非常感谢!
在这种情况下,内置的提取方法将不起作用(因为它们会提取完整路径),但您可以自己轻松完成:
import os
import shutil
destination_dir = './destination'
with zipfile.ZipFile('./tmp.zip', 'r') as z:
# See https://docs.python.org/3/library/zipfile.html#zipfile.ZipFile.infolist
for file_info in z.infolist():
# Only extract regular files
if file_info.is_dir():
continue
file_path = file_info.filename
# Only extract things under 'Content/'
if not file_path.startswith('Content/'):
continue
# Split at slashes, at most one time, and take the second part
# so that we skip the 'Content/' part
extracted_path = file_path.split('/', 1)[1]
# Combine with the destination directory
extracted_path = os.path.join(destination_dir, extracted_path)
print(extracted_path)
# Make sure the directory for the file exists
os.makedirs(os.path.dirname(extracted_path), exist_ok=True)
# Extract the file. Don't use `z.extract` as it will concatenate
# the full path from inside the zip.
# WARNING: This code does not check for path traversal vulnerabilities
# Refer to the big warning inside the ZipFile module for more details
with open(extracted_path, 'wb') as dst:
with z.open(file_info, 'r') as src:
shutil.copyfileobj(src, dst)
在 Python 方面我是菜鸟,我需要帮助!我正在尝试将 zipfile
模块用于最新版本的 Python 3,但在准备提取 zip 文件时我无法尝试排除父目录(文件夹)。
zip 文件目录示例如下所示:
Content/
Content/Runtime/
Content/Runtime/test.txt
Content/Runtime/test.png
Content/Documentation/
Content/Documentation/license.txt
我不知道如何从提取中排除父目录(第一个文件夹)。我只想提取 Runtime & Documentation 目录及其内容。
请帮忙,谢谢,非常感谢!
在这种情况下,内置的提取方法将不起作用(因为它们会提取完整路径),但您可以自己轻松完成:
import os
import shutil
destination_dir = './destination'
with zipfile.ZipFile('./tmp.zip', 'r') as z:
# See https://docs.python.org/3/library/zipfile.html#zipfile.ZipFile.infolist
for file_info in z.infolist():
# Only extract regular files
if file_info.is_dir():
continue
file_path = file_info.filename
# Only extract things under 'Content/'
if not file_path.startswith('Content/'):
continue
# Split at slashes, at most one time, and take the second part
# so that we skip the 'Content/' part
extracted_path = file_path.split('/', 1)[1]
# Combine with the destination directory
extracted_path = os.path.join(destination_dir, extracted_path)
print(extracted_path)
# Make sure the directory for the file exists
os.makedirs(os.path.dirname(extracted_path), exist_ok=True)
# Extract the file. Don't use `z.extract` as it will concatenate
# the full path from inside the zip.
# WARNING: This code does not check for path traversal vulnerabilities
# Refer to the big warning inside the ZipFile module for more details
with open(extracted_path, 'wb') as dst:
with z.open(file_info, 'r') as src:
shutil.copyfileobj(src, dst)