How to Clean Up and Organize Your Files by Month and Year using Python
Do you often find yourself with a cluttered Downloads folder that’s hard to navigate and manage? One way to keep your files organized is to clean up and move them to subdirectories that are named after the month and year they were downloaded. This can help you quickly find files that you downloaded recently, and it can also help you free up space on your hard drive.
In this article, we’ll show you how to use Python to clean up and organize your files by month and year. We’ll provide a Python script that you can use to automate this process, and we’ll explain how you can set it up to run automatically at the start of each month.
Prerequisites
To follow along with this tutorial, you’ll need:
- A computer running Ubuntu (or another Linux distribution)
- Python 3 installed on your system
- Basic knowledge of the command line
Cleaning up and organizing your files with Python
Here’s a Python script that you can use to clean up and organize your files by month and year:
#!/usr/bin/env python3
import os
import shutil
import datetime
# Define the directory to clean up
dir_to_clean = os.path.expanduser('~/Downloads')
# Get the current month and year
now = datetime.datetime.now()
current_month = now.strftime('%B')
current_year = now.strftime('%Y')
# Create the directory for the current month and year if it doesn't exist
current_dir = os.path.join(dir_to_clean, current_year, current_month)
if not os.path.exists(current_dir):
os.makedirs(current_dir)
# Move all files and directories from the directory to the current month and year directory
for item_name in os.listdir(dir_to_clean):
item_path = os.path.join(dir_to_clean, item_name)
if os.path.isfile(item_path) or os.path.islink(item_path):
shutil.move(item_path, current_dir)
elif os.path.isdir(item_path):
# Check if the item is the year directory or a subdirectory of the year directory
if item_name == current_year:
continue
if os.path.abspath(item_path).startswith(os.path.abspath(current_dir)):
continue
# Move the item to the current month and year directory
shutil.move(item_path, os.path.join(current_dir, item_name))
# Print a message to indicate that the cleanup is complete
print(f'{dir_to_clean} cleaned up and organized by month and year.')
This script works by moving all the files in your Downloads directory to a subdirectory that’s named after the current month and year. If the subdirectory doesn’t exist, the script creates it. The script uses the os
and shutil
modules to work with files and directories, and the datetime
module to get the current month and year.
Using the script
To use the script, save it to a file (e.g. cleanup_downloads.py
) in your home directory. You can then run the script from the command line by executing the following command:
python3 ~/cleanup_downloads.py
This will clean up and organize your Downloads directory by moving the files to subdirectories that are named after the current month and year.
Automating the cleanup process with cron
If you want to automate the cleanup process so that it runs automatically at the start of each month, you can use the cron
scheduler in Ubuntu. Here’s how you can set it up:
- Open the crontab configuration file by running the following command in the terminal:
crontab -e
- If this is your first time using crontab, you may be asked to choose a text editor. Choose your preferred text editor.
- Add the following line to the bottom of the file:
0 0 1 * * /usr/bin/python3 /home/username/cleanup_downloads.py
- This will run the
cleanup_downloads.py
on the first day of every month. Replace username with your actual username, and replace /home/username/cleanup_downloads.py with the full path to the script. - Save and exit the crontab file.
Modifying the script for other directories
If you want to clean up and organize other directories in your home directory, you can modify the script to use a different directory path. Simply change the value of the downloads_dir
variable at the top of the script to the path of the directory you want to clean up.
You can also modify the script to use a different format for the subdirectory names. For example, you can use the abbreviated month name and the last two digits of the year by using the %b
and %y
format codes instead of %B
and %Y
, respectively.
Conclusion
Using Python to clean up and organize your files by month and year can save you time and help you keep your files organized. The script we’ve provided is a simple and effective way to automate this process, and it can be easily customized to work with other directories in your home directory.
We hope you found this article helpful, and we encourage you to experiment with the script and customize it to meet your needs. If you have any questions or feedback, feel free to leave a comment below.
Code
Here’s the full code for the Python script:
#!/usr/bin/env python3
import os
import shutil
import datetime
# Define the directory to clean up
dir_to_clean = os.path.expanduser('~/Downloads')
# Get the current month and year
now = datetime.datetime.now()
current_month = now.strftime('%B')
current_year = now.strftime('%Y')
# Create the directory for the current month and year if it doesn't exist
current_dir = os.path.join(dir_to_clean, current_year, current_month)
if not os.path.exists(current_dir):
os.makedirs(current_dir)
# Move all files and directories from the directory to the current month and year directory
for item_name in os.listdir(dir_to_clean):
item_path = os.path.join(dir_to_clean, item_name)
if os.path.isfile(item_path) or os.path.islink(item_path):
shutil.move(item_path, current_dir)
elif os.path.isdir(item_path):
# Check if the item is the year directory or a subdirectory of the year directory
if item_name == current_year:
continue
if os.path.abspath(item_path).startswith(os.path.abspath(current_dir)):
continue
# Move the item to the current month and year directory
shutil.move(item_path, os.path.join(current_dir, item_name))
# Print a message to indicate that the cleanup is complete
print(f'{dir_to_clean} cleaned up and organized by month and year.')
Remember to replace ~/Downloads
with the path to the directory you want to clean up.
Warning: Before using this script to clean up and organize your files, please make sure to read and understand the code. We take no responsibility for any accidental deletion or loss of files that may occur as a result of running this script. Use this script at your own risk, and make sure to back up your files before running it.