Reputation: 1734
I am creating a script to backup an entire program with multiple folders, databases and hundred of folders and files. For that I use this code and it works great (slightly editted for SO).
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))
The problem:
With this code, my prints just show me the base folder instead of each file, like:
Saving: File1.txt
Saving: File2.mp3
Saving: Folder_sounds
Saving: something.json
Let's pretend that folder_sounds
is a folder with thousands of files. The script will take a lot of time (freezing my GUI) while adding up that folder files to the tar file but I don't really know the progress of it because the print is not showing me each file individually. That's the problem.
What I tried:
I tried to get the full path of each file at part 1 of my code, however that would add the files to the tarfile without creating folders inside the tarfile or adding the files inside its respective folder. It was a mess because all the files were in the same place.
Desired solution:
1: Print each file name as they are being added to the tarfile.
2: Store all the files in the tarfile without breaking the tree of folders where each file belongs.
Upvotes: 1
Views: 165
Reputation: 1734
Answering my own question until a better answer is provided:
# 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))
Upvotes: 0