从一个目录创建一个仅包含 .pdf 和 .xml 文件的 zip
Create a zip with only .pdf and .xml files from one directory
我很想知道如何只压缩主目录中的所有 pdf,而不包括子文件夹。
我已经尝试过多次更改代码,但没有成功实现我想要实现的目标。
import zipfile
fantasy_zip = zipfile.ZipFile('/home/rob/Desktop/projects/zenjobv2/archivetest.zip', 'w')
for folder, subfolders, files in os.walk('/home/rob/Desktop/projects/zenjobv2/'):
for file in files:
if file.endswith('.pdf'):
fantasy_zip.write(os.path.join(folder, file), os.path.relpath(os.path.join(folder,file), '/home/rob/Desktop/projects/zenjobv2/'), compress_type = zipfile.ZIP_DEFLATED)
elif file.endswith('.xml'):
fantasy_zip.write(os.path.join(folder, file), os.path.relpath(os.path.join(folder,file), '/home/rob/Desktop/projects/zenjobv2/'), compress_type = zipfile.ZIP_DEFLATED)
fantasy_zip.close()
我希望仅使用 zenjobv2 folder/directory 中的 .pdf 和 .xml 文件创建 zip,而不包含任何其他 folders/subfolders.
您正在使用 os.walk()
. It sounds like you want to just look at the files in a given directory. For that, consider os.scandir()
遍历整个目录树,其中 returns 是给定目录中所有文件和子目录的迭代器。您只需要过滤掉目录元素:
root = "/home/rob/Desktop/projects/zenjobv2"
for entry in os.scandir(root):
if entry.is_dir():
continue # Just in case there are strangely-named directories
if entry.path.endswith(".pdf") or entry.path.endswith(".xml"):
# Process the file at entry.path as you see fit
我很想知道如何只压缩主目录中的所有 pdf,而不包括子文件夹。
我已经尝试过多次更改代码,但没有成功实现我想要实现的目标。
import zipfile
fantasy_zip = zipfile.ZipFile('/home/rob/Desktop/projects/zenjobv2/archivetest.zip', 'w')
for folder, subfolders, files in os.walk('/home/rob/Desktop/projects/zenjobv2/'):
for file in files:
if file.endswith('.pdf'):
fantasy_zip.write(os.path.join(folder, file), os.path.relpath(os.path.join(folder,file), '/home/rob/Desktop/projects/zenjobv2/'), compress_type = zipfile.ZIP_DEFLATED)
elif file.endswith('.xml'):
fantasy_zip.write(os.path.join(folder, file), os.path.relpath(os.path.join(folder,file), '/home/rob/Desktop/projects/zenjobv2/'), compress_type = zipfile.ZIP_DEFLATED)
fantasy_zip.close()
我希望仅使用 zenjobv2 folder/directory 中的 .pdf 和 .xml 文件创建 zip,而不包含任何其他 folders/subfolders.
您正在使用 os.walk()
. It sounds like you want to just look at the files in a given directory. For that, consider os.scandir()
遍历整个目录树,其中 returns 是给定目录中所有文件和子目录的迭代器。您只需要过滤掉目录元素:
root = "/home/rob/Desktop/projects/zenjobv2"
for entry in os.scandir(root):
if entry.is_dir():
continue # Just in case there are strangely-named directories
if entry.path.endswith(".pdf") or entry.path.endswith(".xml"):
# Process the file at entry.path as you see fit