Skip to content

Instantly share code, notes, and snippets.

@Xseron
Last active April 5, 2024 18:05
Show Gist options
  • Save Xseron/d99b3f3b3e796c8e073f28c142fc8ec1 to your computer and use it in GitHub Desktop.
Save Xseron/d99b3f3b3e796c8e073f28c142fc8ec1 to your computer and use it in GitHub Desktop.
FTP Autoload is a robust software solution designed to streamline the process of transferring files between local systems and remote servers via FTP (File Transfer Protocol). This project aims to automate and simplify file transfer tasks, enhancing efficiency and reducing manual intervention.
from ftplib import FTP, error_perm
import os.path, os
def remove_ftp_dir(ftp, path):
"""Recursively delete all files and directories within the given directory on the FTP server."""
for (name, properties) in ftp.mlsd(path=path):
print(name)
if name in ['.', '..'] or name.startswith(".") or name.startswith(".."):
continue
elif properties['type'] == 'file':
ftp.delete(f"{path}/{name}")
print(f"Deleted file: {path}/{name}")
elif properties['type'] == 'dir':
remove_ftp_dir(ftp, f"{path}/{name}")
print(f"Deleted directory: {path}")
def placeFiles(ftp, path):
"""Recursively upload files and create directories on the FTP server."""
for name in os.listdir(path):
localpath = os.path.join(path, name)
if os.path.isfile(localpath):
print("STOR", name, localpath)
ftp.storbinary('STOR ' + name, open(localpath, 'rb'))
elif os.path.isdir(localpath):
print("MKD", name)
try:
ftp.mkd(name)
except error_perm as e:
if not e.args[0].startswith('550'): # ignore "directory already exists"
raise
print("CWD", name)
ftp.cwd(name)
placeFiles(ftp, localpath) # Recursively upload files in the directory
print("CWD", "..")
ftp.cwd("..")
def main():
# FTP server details
ftp_host = ''
ftp_user = ''
ftp_pass = ''
# Local directory with files to upload
local_dir = '../www/production'
# Connect to FTP server
ftp = FTP(ftp_host)
ftp.login(user=ftp_user, passwd=ftp_pass)
# Directory on FTP server to upload files to
ftp_dir = './'
# Upload files and directories
placeFiles(ftp, local_dir)
# Close FTP connection
ftp.quit()
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment