Python解压文件排除特定fi

2024-04-25 02:06:30 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在使用python zipfile内置模块。你知道吗

我可以解压缩一个文件,但我只需要排除一个文件。你知道吗

有办法吗? 因为我在使用extractall(),所以我也得到了排除的文件。你知道吗

  with ZipFile(zip_file_name, 'r') as zipObj:
        # Extract all the contents of zip file in current directory
        zipObj.extractall()

Tags: 模块文件nameaswithextractallzip
1条回答
网友
1楼 · 发布于 2024-04-25 02:06:30

要做到这一点,我认为你需要有这些步骤。你知道吗

  1. 列出要“提取”的目标文件列表
  2. 添加“If”条件或regex以指定要提取的唯一文件
    with ZipFile(zip_file_name, 'r') as zipObj:
        # Get a list of all archived file names from the zip
        listOfFileNames = zipObj.namelist()
        # Iterate over the file names
        for fileName in listOfFileNames:
            #check the excluding file condition.
            if fileName is 'FILE_TO_BE_EXCLUDED.txt':
                continue
            zipObj.extract(fileName, 'path_for_extracting')

我的参考号是here. 希望这能对你有所帮助。你知道吗

相关问题 更多 >