备份文件夹树,但单独打印每个文件

2024-04-26 09:43:05 发布

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

我正在创建一个脚本来备份一个包含多个文件夹、数据库和数百个文件夹和文件的整个程序。为此,我使用了这段代码,它工作得很好(为此略加编辑)。你知道吗

import tarfile
import datetime
PATH_PROGRAM = os.getcwd()

# Part 1: Get list of files
files_to_save = []
for file_name in listdir(PATH_PROGRAM):
    if not file_name == "Backups Folder": # Exclude the folder where we store them
        files_to_save.append(file_name)

# Part 2: Get name for new backup file
date_now = str(datetime.datetime.now())[:10]
backupfile = "{0}\\Backups Folder\\{1}.tar.gz".format(PATH_PROGRAM, date_now)

# Part 3: Add all files to new backup
with tarfile.open(backupfile, "w:gz") as tar:
    for file in files_to_save:
        print("Saving: {0}".format(file))
        tar.add("{0}\\{1}".format(PATH_PROGRAM, file))

问题:
使用此代码,我的打印只显示基本文件夹,而不是每个文件,如:

Saving: File1.txt
Saving: File2.mp3
Saving: Folder_sounds
Saving: something.json

让我们假设folder_sounds是一个包含数千个文件的文件夹。脚本在将文件夹文件添加到tar文件时会花费很多时间(冻结我的GUI),但我不知道它的进度,因为打印没有单独显示每个文件。这就是问题所在。你知道吗

我尝试的:

我在代码的第1部分中尝试获取每个文件的完整路径,但是这会将文件添加到tarfile中,而不会在tarfile中创建文件夹,也不会将文件添加到相应的文件夹中。因为所有的文件都在同一个地方,所以很乱。你知道吗

所需解决方案:

1:在将每个文件添加到tar文件时打印它们。
2:将所有文件存储在tarfile中,而不破坏每个文件所属的文件夹树。你知道吗


Tags: 文件topath代码name文件夹datetimesave
1条回答
网友
1楼 · 发布于 2024-04-26 09:43:05

回答我自己的问题直到得到更好的答案:

# Part 1 became a function
def get_all_files_for_backup(self):
    """Returns a Dict"""
    dict_of_diles = {}
    for dirpath, _, filenames in os.walk(PATH_PROGRAM):
        if not "Backups Folder" in dirpath:  # Exclude this folder
            for filename in filenames:
                if dirpath in dict_of_diles:
                    # If folder exists, append this file to it
                    dict_of_diles[dirpath].append(filename)
                else:
                    # Create a new item in the dict for this folder
                    dict_of_diles[dirpath] = [filename]
    return dict_of_diles

files_to_save = self.get_all_files_for_backup()

# Part 3: We create the tree structure in the tarfile before adding files to it
with tarfile.open(backupfile, "w:gz") as tar:
    for folder, files in files_to_save.items():
        # Create the right tree folders inside the tar file
        relative_folder = folder.replace(PATH_PROGRAM, "")
        if relative_folder.startswith("\\"):
            relative_folder = relative_folder[1:]
            tarfolder = tarfile.TarInfo(relative_folder)
            tarfolder.type = tarfile.DIRTYPE
            tar.addfile(tarfolder)
        # Add the files
        for file in files:
            full_path = os.path.join(folder, file)
            print(full_path)
            # Don't add an arcname if relative folder is none
            if relative_folder == "":
                tar.add(full_path, arcname=file)
            else:
                # Args for tar.add are: Copy from absolute path, Copy to that tar folder.
                tar.add(full_path, arcname="{0}\\{1}".format(relative_folder, file))

相关问题 更多 >