简体   繁体   English

如何在python中压缩相同前缀的文件?

[英]How to zip up files of same prefix in python?

If I have a directory "C:\\Files" that contains a bunch of files: 如果我有一个目录“C:\\ Files”,其中包含一堆文件:

A_File1 A_File2 B_File1 B_File2 A_File1 A_File2 B_File1 B_File2

What is the best way to iterate through the files to zip up any file with the same prefix into a single zip file? 迭代文件以将具有相同前缀的任何文件压缩到单个zip文件中的最佳方法是什么? For instance, output would be "A.zip" and "B.zip" and their associated files. 例如,输出将是“A.zip”和“B.zip”及其相关文件。

I know how to iterate through the files: 我知道如何遍历文件:

for item in os.listdir("C:\FILES"):

But I do not know what the best way to zip up the files is or if there is some python library that helps with that. 但我不知道压缩文件的最佳方法是什么,或者是否有一些python库可以帮助解决这个问题。

Use the glob module from the standard library, instead of os.listdir: 使用标准库中的glob模块,而不是os.listdir:

from glob import glob

for file in glob("C:\\FILES\\A_File*"):
    ...

(If you are using backslashes to separate dirs, use two, because a single one is a escape character in Python strings) (如果使用反斜杠分隔dirs,请使用两个,因为一个是Python字符串中的转义字符)

This assumes that C:\\FILES\\ contains only files that you want to compress, and each file is in the format PREFIX_* . 这假定C:\\FILES\\只包含要压缩的文件,每个文件的格式为PREFIX_*

import glob, os
import zipfile
import defaultdict

# We organize the file names in a dict to
# make writing out the zip files easier

d = defaultdict(list)

for fname in glob.glob(r'C:\FILES\*'):
   d[fname.split('_')[0]].append(fname)

# Now we go through each file by letter
for letter in d:
    with zipfile.ZipFile(letter+'.zip','w') as f:
        for filename in d[letter]:
            f.write(filename, os.path.basename(filename), zipfile.ZIP_DEFLATED)
for fname in glob.glob('c:\files\A_*'):
    print fname

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM