I haven't done much Python in the past, so I was wondering if you could look over my code and make sure everything is up to snuff?
I know programs that do this exist already, but I am having a challenge with my friend on who can build a simple backup system for our college files.
I have run the code through Pylint and a PEP8 code checker, and fixed the errors it produced.
"""
Basic Python Backup
Nat Zimmermann
"""
# SETTINGS
TIME_FORMAT = '%d-%m-%Y-%H:%M:%S'
MAX_DIRS = 5
import shutil
from datetime import datetime
from os import walk
def get_dirs(dirs):
"""Get all immidiate directories in the current directory"""
converted_dirs = []
if len(dirs) > MAX_DIRS:
test_dir = None
for test_dir in dirs:
try:
app_dir = datetime.strptime(test_dir, TIME_FORMAT)
converted_dirs.append(app_dir)
except ValueError:
print("Directory %s does not match date format, ignoring..."
% test_dir)
return converted_dirs
def remove_dirs():
"""Remove the oldest directories"""
dirs = walk('.').__next__()[1]
converted_dirs = get_dirs(dirs)
if converted_dirs:
try:
oldest_dir = datetime.strftime(min(converted_dirs), TIME_FORMAT)
shutil.rmtree(oldest_dir)
print("Removed old directory %s" % oldest_dir)
remove_dirs()
except IOError as error:
print(error)
def copy_directory():
"""Copiy a given directory to a new directory with the current date"""
old_directory = input("Directory to copy: ")
new_directory = datetime.now().strftime(TIME_FORMAT)
print("Copying...")
try:
shutil.copytree(old_directory, new_directory)
print("Successfully copied files to directory %s" % new_directory)
except IOError as error:
print(error)
copy_directory()
copy_directory()
remove_dirs()