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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
import ftplib import os from datetime import datetime, timedelta # Define your FTP server details ftp_host = "your_ftp_server" ftp_user = "your_username" ftp_password = "your_password" # Local file path and destination directory on the server local_file = "path/to/your/file.txt" remote_dir = "/uploaded" # Function to upload a file def upload_file(ftp, local_file, remote_dir): with open(local_file, "rb") as file: ftp.cwd(remote_dir) # Change directory to "uploaded" ftp.storbinary(f"STOR {os.path.basename(local_file)}", file) print(f"File {local_file} uploaded successfully!") # Function to clean up old files (more than 30 days) def cleanup_old_files(ftp, remote_dir, days_old=30): ftp.cwd(remote_dir) now = datetime.utcnow() for filename in ftp.nlst(): file_date = ftp.mtime(filename) # Get file modification time if (now - file_date) > timedelta(days=days_old): ftp.delete(filename) print(f"Deleted old file: {filename}") # Connect to FTP server ftp = ftplib.FTP(ftp_host) ftp.login(ftp_user, ftp_password) # Upload the file upload_file(ftp, local_file, remote_dir) # Clean up old files cleanup_old_files(ftp, remote_dir) # Close the connection ftp.quit() |
Explanation:
- Import Libraries: We import
ftplib
for FTP interaction,os
for file paths, anddatetime
for managing file age. - Define Variables: Replace placeholders with your FTP server details, local file path, and desired remote directory for uploads.
- 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 usingSTORBINARY
. - Cleanup Function: This function iterates through files in
remote_dir
, retrieves their modification time usingMTIME
, 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 usingDELETE
. - 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!