Here is the script that I would like reviewed for the following:
- Best practices and design pattern usage
- Correctness in unanticipated cases
- Possible error checking
Any streamline updates/ideas are welcome!
import os
import tarfile
import time
# Create backup filename
timestr = time.strftime("%Y-%m-%d:%H%M%S")
output_filename = "minecraft-backup-" +timestr
backup_filename = output_filename + '.tar.gz'
# Locataion of final backup files
destination_dir = "/Users/username/Desktop/backup/"
destination_file = destination_dir + backup_filename
# Directory to backup
backup_dir = '/Users/username/.config/'
# Backup files
with tarfile.open(destination_file, "w:gz") as tar:
for fn in os.listdir(backup_dir):
p = os.path.join(backup_dir, fn)
tar.add(p, arcname=fn)
# Remove files older than 7 days
current_time = time.time()
daysToDelete = 7
for dirpath,_,filenames in os.walk(destination_dir):
for f in filenames:
fileWithPath = os.path.abspath(os.path.join(dirpath, f))
creation_time = os.path.getctime(fileWithPath)
print("file available:",fileWithPath)
if (current_time - creation_time) // (24 * 3600) >= daysToDelete:
os.unlink(fileWithPath)
print('{} removed'.format(fileWithPath))
print("\n")
```