Python script to clean out old files from a repository
This is just a brain dump of some things about your code that I hope you find useful.

Automated tools

Automated tools can help you maintain your code more easily.

The first ones I install whenever I start a new Python project, are:


black — The uncompromising Python code formatter
isort — A Python utility to sort imports
flake8 — A tool to enforce PEP 8 compliance (among other things)


I ran them on your code with the following options:


pipenv run black clean_old_files.py
pipenv run flake8 --max-line-length 88 clean_old_files.py
pipenv run isort clean_old_files.py -m=3 -tc -l=88


(An aside, I'm using Pipenv to manage my virtual environments. They're unmissable once you start getting into larger projects with external dependencies, and other contributors.)

Noisy comments

You have several comments that explain what the code does, rather than why.

For instance this:

# Gets the current time from the time module
now = time.time()
# Timer of when to purge files
cutoff = now - (14 * 86400)


which could be rewritten as:

# Timer of when to purge files
cutoff = time.time() - (14 * 86400)


which removes one "what" comment, and inlines a temporary variable.

My advice would be to go over all the comments in your script, and see which are "what" comments, and which are "why" comments.

Code structure

A minor thing to start off, I'd move the main function to the bottom of the script, right before if __name__ == "__main__", which, conceptually makes most sense to me.

You are mutating global variables from functions, which makes it harder to follow your program structure as-is.

On a related note, you also have global All_dest_dirty, which is a typo.

Globals

Instead of, for instance:

all_dest_dirty = []

def dest_files():
    for dest_root, dest_subdirs, dest_files in os.walk(dest):
        for f in dest_files:
            global all_dest_dirty
            all_dest_dirty.append(f)


prefer:

def dest_files():
    dirty_files = []
    for dest_root, dest_subdirs, dest_files in os.walk(dest):
        for f in dest_files:
            dirty_files.append(f)

    return dirty_files

all_dest_dirty = dest_files()


Closing remarks

Correct me if I'm wrong, but from

all_dest_clean = list(dict.fromkeys(all_dest_dirty))


I think that you're trying to remove duplicates from a list?

You can write that as

all_dest_clean = set(all_dest_dirty)


which uses the set data type, which prevents any duplicates. Plus, it's less typing, and also clearer. :-)



I hope you found my mini review helpful. You're doing great for a beginner! Keep it up. Also, let me know if anything is unclear.