This script to locate torrent files in a given directory, and if they have been seeding for 2 weeks, delete the files.
I realize the comments are overboard. The reason for that is it's being shared with people who don't program.
What should I have done differently?
import os # for listdir(), path(), remove(), and getctime()
from time import time, ctime, sleep
import datetime as dt # datetime to compare dates
# Path to torrents
torrents_folder = r'C:\users\user\torrents\folder'
# Torrent files as a list
torrents = os.listdir(torrents_folder)
# Months will be used to swap month prefix and corresponding month number
months = {"Jan": "01", "Feb": "02", "Mar": "03", "Apr": "04", "May": "05", "Jun": "06", "Jul": "07", "Aug": "08",
"Sep": "09", "October": "10", "Nov": "11", "Dec": "12", }
# Set the number of days to elapse from creation date before file is deleted
days_to_wait = dt.timedelta(days=14)
# function to remove torrent
def torrent_remover(month):
# Format the date, casting month to string
start_date = dt.datetime.strptime(str(month), "%m %d %Y")
# Elapsed Time. See line 12
end_date = start_date + days_to_wait
# Today's date for comparison
today = dt.datetime.today()
if end_date <= today:
print("Removing: ", torrent, "\tTorrent expired on", end_date)
# Create full path from the torrent folder and torrent file
torrent_files = os.path.join(torrents_folder, torrent)
# Delete the torrents
os.remove(torrent_files)
else:
print("Nothing to remove")
exit()
# Start the loop
for torrent in torrents:
if torrent.endswith(".torrent"):
# Get date time from each file then join file and folder to create full path
date = ctime(os.path.getctime(os.path.join(torrents_folder, torrent)))
# Remove the timestamp
remove_timestamp = date.replace(date[11:19], "")
# Remove the day
new_date = remove_timestamp.replace(remove_timestamp[0:4], "")
# Return single value in list
month = [month for month in months if month in new_date][0]
if month in new_date:
# Returns the month number when passed month key
month_number = months[month]
# Replace the month prefix with the corresponding number
reformatted_date = new_date.replace(month, month_number)
# Send to delete function to be deleted.
torrent_remover(reformatted_date)