如何在 Python 中重命名解压缩的文件?

Posted

技术标签:

【中文标题】如何在 Python 中重命名解压缩的文件?【英文标题】:How to rename unzipped files in Python? 【发布时间】:2020-12-07 11:35:43 【问题描述】:

我有以下结构:

Folder1
      ZZ-20201201-XX.zip
Folder2
      XX-20201201-XX.zip
      XX-20201202-XX.zip
Folder3
      YY-20201201-XX.zip
      YY-20201202-XX.zip

使用下面的代码,我创建了 Folder1, Folder2Folder3 的对应项,并直接解压缩这 3 个文件夹中的压缩文件。所以我收到了这个:

Folder1
      ZZ-.txt

Folder2
      XX-.txt
     
Folder3
      YY.txt

如您所见,文件一旦解压缩就会丢失日期,因此如果文件夹包含 2 个压缩文件,它们将获得相同的名称,因此文件将被重写。现在我想在文件解压缩后将压缩文件的日期添加到文件中。我该怎么做?

import fnmatch

pattern = '*.zip'
for root, dirs, files in os.walk(my_files): 
    for filename in fnmatch.filter(files, pattern): 
        path = os.path.join(root, filename)
        date_zipped_file = re.search('-(.\d+)-', filename).group(1) #<-- this is the date of the zipped files and I want this to be included in the name of the unzipped files once they get unzipped.

        # Store the new directory so that it can be recreated
        new_dir = os.path.normpath(os.path.join(os.path.relpath(path, start=my_files), ".."))

        # Join your target directory with newly created directory
        new = os.path.join(counter_part, new_dir)

        # Create those folders, works even with nested folders
        if (not os.path.exists(new)):
            os.makedirs(new)

        zipfile.ZipFile(path).extractall(new) 

我想要的结果:

Folder1
      ZZ-20201201.txt
Folder2
      XX-20201201.txt
      XX-20201202.txt
Folder3
      YY-20201201.txt
      XX-20201202.txt

【问题讨论】:

【参考方案1】:

您可以在解压缩每个文件夹后重命名文件。像这样的:

#get all files in that unzipped folder
files = os.listdir(path)

#rename all files in that dir
for file in files:
    filesplit = os.path.splitext(os.path.basename(file))
    os.rename(os.path.join(path, file), os.path.join(path, filesplit[0]+'_'+date_zipped_file+filesplit[1]))

但这也会重命名实际上可能已经在名称中包含date 的文件。因此,您还需要集成检查文件是否已重命名。通过维护带有文件名的list 或简单的regex 来查找'_'和'.'之间的8位字符串,例如text_20201207.txt。

#get all files in that unzipped folder
    files = os.listdir(path)
    
    #rename all files in that dir
    for file in files:
        filesplit = os.path.splitext(os.path.basename(file))
        if not re.search(r'_\d8.', file):
            os.rename(os.path.join(path, file), os.path.join(path, filesplit[0]+'_'+date_zipped_file+filesplit[1]))

您的最终解决方案将如下所示:

import fnmatch

pattern = '*.zip'
for root, dirs, files in os.walk(my_files): 
    for filename in fnmatch.filter(files, pattern): 
        path = os.path.join(root, filename)
        date_zipped_file = re.search('-(.\d+)-', filename).group(1) #<-- this is the date of the zipped files and I want this to be included in the name of the unzipped files once they get unzipped.

        # Store the new directory so that it can be recreated
        new_dir = os.path.normpath(os.path.join(os.path.relpath(path, start=my_files), ".."))

        # Join your target directory with newly created directory
        new = os.path.join(counter_part, new_dir)

        # Create those folders, works even with nested folders
        if (not os.path.exists(new)):
            os.makedirs(new)

        zipfile.ZipFile(path).extractall(new) 

        #get all files in that unzipped folder
        files = os.listdir(new)
    
        #rename all files in that dir
        for file in files:
            filesplit = os.path.splitext(os.path.basename(file))
            if not re.search(r'_\d8.', file):
                os.rename(os.path.join(new, file), os.path.join(new, filesplit[0]+'_'+date_zipped_file+filesplit[1]))

【讨论】:

以上是关于如何在 Python 中重命名解压缩的文件?的主要内容,如果未能解决你的问题,请参考以下文章

如何在mac上正确压缩文件以通过python从s3解压缩?

使用批处理解压缩和重命名文件 (Windows)

python 如何解压缩以ZStandard压缩格式编写的文件。

python中如何压缩和解压缩文件

如何在 Python3 中解压缩使用 PKZIP 以外的算法加密的文件?

如何使用shell调用Winrar压缩及解压缩文件