Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/320.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PythonZipFile库-创建一个只包含一个目录中的.pdf和.xml文件的zip_Python_Python 3.x_If Statement_Zipfile_Os.walk - Fatal编程技术网

PythonZipFile库-创建一个只包含一个目录中的.pdf和.xml文件的zip

PythonZipFile库-创建一个只包含一个目录中的.pdf和.xml文件的zip,python,python-3.x,if-statement,zipfile,os.walk,Python,Python 3.x,If Statement,Zipfile,Os.walk,我想知道如何只压缩主目录中的所有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

我想知道如何只压缩主目录中的所有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文件夹/目录中的.pdfs和.xml文件创建zip,而不包括任何其他文件夹/子文件夹

(已编辑)解决方案:

import os, glob
import zipfile

fantasy_zip = zipfile.ZipFile('/home/rob/Desktop/projects/zenjobv2/archivetest.zip', 'w')

root = "/home/rob/Desktop/projects/zenjobv2"

for file in os.listdir(root):
    if file.endswith('.pdf') or file.endswith(".xml"):
        fantasy_zip.write(file)
fantasy_zip.close()
使用OP中的新信息进行更新: 您正在使用循环遍历整个目录树。听起来您只想查看给定目录中的文件。为此,考虑返回一个给定目录中所有文件和子目录的迭代器。您只需过滤掉目录中的元素:

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
之前的回答基于对问题的理解不足: 您在调用
ZipFile.write()
时隐式指定了,这将在存档中创建一个文件,该文件的路径与您给定的路径、子目录以及所有内容完全相同。如果要添加到归档文件的文件位于路径
/home/rob/Desktop/projects/zenjobv2/subdir1/subdir2/file.pdf
,则使用的定义,代码有效地转换为:

fantasy_zip.write("/home/rob/Desktop/projects/zenjobv2/subdir1/subdir2/file.pdf",
                  arcname="subdir1/subdir2/file.pdf",
                  compress_type=zipfile.ZIP_DEFLATED)
由于
arcname
参数中有目录分隔符,因此该文件将添加到名为
subdir1/subdir2
的子目录中的存档中

您可能打算这样做:

fantasy_zip.write(os.path.join(folder, file), arcname=file)

这将使目录结构不在存档中。不过,请注意,同名文件将被覆盖。

谢谢,我会检查一下。您知道如何只在主目录中查找.pdf和.xml,而不在subdir1和subdir2中查找.pdf/.xml文件吗。提前感谢。您正在使用
os.walk()
遍历子目录,这将遍历整个目录树。换个角度考虑。