Let’s create a Python script to back up your Downloads folder to another drive. The backup will be organized by month and year, and after the backup, the original files will be deleted.
This script can be run manually or scheduled to run automatically based on your preference.
Step 1: Install Required Libraries
First, we’ll need Python installed on your computer. If you haven’t installed Python yet, you can download and install it from python.org.
Next, we’ll use the built-in shutil
library for copying and deleting files, so no extra installation is required for this part.
Step 2: Setting Up the Script
- Open your code editor: You can use any text editor like Notepad++, Visual Studio Code, or even the basic Notepad.
- Create a new Python file: Save it as
backup_downloads.py
.
Step 3: Write the Python Script
Hereโs what the script will do:
- Determine the current month and year to create folder names like “January 2024”.
- Copy all files from the Downloads folder to the backup location.
- Delete the original files after copying.
Script Details
pythonCopy codeimport shutil
import os
from datetime import datetime
def backup_downloads():
# Step A: Set the paths
downloads_path = "C:/Users/YourUsername/Downloads" # Change 'YourUsername' to your actual username
backup_drive_path = "D:/Backups" # Change 'D:' to your actual backup drive letter
# Step B: Create a backup folder name based on the current month and year
now = datetime.now()
folder_name = now.strftime("%B %Y") # This will be something like 'January 2024'
full_backup_path = os.path.join(backup_drive_path, folder_name)
# Step C: Create the backup directory if it doesn't already exist
if not os.path.exists(full_backup_path):
os.makedirs(full_backup_path)
# Step D: Copy files from Downloads to the backup folder
for filename in os.listdir(downloads_path):
file_path = os.path.join(downloads_path, filename)
if os.path.isfile(file_path): # Make sure it's a file, not a directory
shutil.copy(file_path, full_backup_path)
# Step E: Delete the original files after backup
for filename in os.listdir(downloads_path):
file_path = os.path.join(downloads_path, filename)
if os.path.isfile(file_path): # Ensure it's a file
os.remove(file_path)
print("Backup complete and original files deleted.")
# Step F: Run the backup function
if __name__ == "__main__":
backup_downloads()
Explanation of the Script:
- Step A: Define the paths for your Downloads folder and where you want to back up the files.
- Step B: We use
datetime
to get the current month and year for naming the backup folder. - Step C: Checks if the backup folder exists; if not, it creates it.
- Step D: Goes through each file in the Downloads folder and copies it to the backup folder.
- Step E: Deletes the original files from the Downloads folder after they are backed up.
- Step F: The last part runs the backup function if the script is executed directly.
Important Warning: Test Before Use
Before using this script to back up and delete files from your Downloads folder or any other important directory, test it with a demo folder first.
This will help you confirm that the script functions as expected without risking any important data. Follow these steps to safely test the script:
- Create a Demo Folder: Make a new folder on your desktop or another safe location. Name it something like “TestDownloads”.
- Add Sample Files: Copy some files into this demo folder. These files should be ones you can afford to lose, as they will be deleted during the testing process.
- Adjust the Script for Testing: Modify the script’s
downloads_path
variable to point to your demo folder. For example:pythonCopy codedownloads_path = "C:/Users/YourUsername/Desktop/TestDownloads"
- Run the Script: Follow the steps previously described to run your script. Check the backup location to ensure the files were copied and then confirm they were deleted from the demo folder.
- Review Results: Make sure the files are correctly backed up to the designated backup folder and that they are no longer in the demo folder.
- Reset the Script: Once you confirm the script works as intended with your demo folder, change the
downloads_path
back to your actual Downloads folder or the original intended folder.
Step 4: Run the Script
To run your script:
- Open Command Prompt on Windows.
- Navigate to the directory where your script is saved. You can do this by typing
cd path_to_your_script_directory
and pressing Enter. - Run the script by typing
python backup_downloads.py
and pressing Enter.
Conclusion
Creating a Python script to automate the backup of your Downloads folder can significantly streamline your file management process, ensuring that your data is safely archived and reducing the clutter on your primary storage.
By following the steps outlined above, you’ve learned how to write a Python script that not only backs up your files efficiently but also organizes them into neatly labeled folders based on the month and year.
Additionally, the script’s capability to delete the original files after a successful backup helps maintain a clean and organized digital environment.
Testing the script with a demo folder before deploying it to handle important files is crucial. This precautionary step ensures that the script functions as intended without any surprises, safeguarding your valuable data against accidental loss.
The visual guide provided further aids in understanding the setup and testing phases, making the implementation straightforward even for those with basic technical skills.
As you become more comfortable with the script, you can consider enhancing its functionality. Potential improvements could include:
- Adding error handling to manage exceptions and ensure the script runs smoothly under various conditions.
- Implementing logging to keep track of what files were backed up and when, providing a clear audit trail.
- Extending the script to handle multiple source folders or integrating cloud storage options for an even more robust backup solution.

Ishaan is a passionate programmer with a knack for simplifying complex ideas. With expertise in Python and PHP, he enjoys creating small projects that help others learn and apply coding skills practically. Through his articles, Ishaan aims to inspire and educate budding developers.