Automating File Uploads and Cleanup with Python and FTP

Keeping your FTP server organized can be a chore, especially if you frequently upload files. This blog post will guide you through creating a Python script that automates pushing files, keeps your “uploaded” folder clean, and removes any files older than 30 days.

Prerequisites:

  • Basic understanding of Python
  • FTP server access (host, username, password)

The Script:

We’ll use the ftplib module to interact with the FTP server and the os module for file operations. Here’s the script breakdown:

 

Explanation:

  1. Import Libraries: We import ftplib for FTP interaction, os for file paths, and datetime for managing file age.
  2. Define Variables: Replace placeholders with your FTP server details, local file path, and desired remote directory for uploads.
  3. Upload Function: This function opens the local file in binary mode, changes the working directory on the server to remote_dir, and uploads the file using STORBINARY.
  4. Cleanup Function: This function iterates through files in remote_dir, retrieves their modification time using MTIME, and compares it with the current date minus the specified number of days (defaults to 30). If a file is older than the threshold, it’s deleted using DELETE.
  5. Main Script: We connect to the FTP server, upload the file using our function, clean up old files, and close the connection.

Running the Script:

Save the script as ftp_upload.py and run it from your terminal using python ftp_upload.py. Remember to update the script with your specific information.

Security Note:

This script stores credentials directly in the code. For production use, consider environment variables or secure credential management techniques.

Further Enhancements:

  • Error handling for upload failures or connection issues.
  • Logging for successful uploads and deletions.
  • Uploading entire directories with recursive functions (refer to online resources).

This Python script automates your FTP file uploads and keeps your server organized. With a little customization, you can adapt it to fit your specific needs!

Leave a Reply

Your email address will not be published. Required fields are marked *