Python and FTP

Wednesday, August 01, 2007 08:36 AM

I decided to automate some file transferring I normally do the old fashioned way via FTP. I decided this would be a great job for python. I came up with the following code....

from ftplib import FTP
#You probably want to initialize the variables below....
FTP_ADDRESS = "..."
FTP_USERNAME = "..."
FTP_PASSWORD = "..."
REMOTE_DIRECTORY = "... "
REMOTE_FILE = "..."
VERSION = "..."
ftp = FTP(FTP_ADDRESS)
ftp.login(FTP_USERNAME, FTP_PASSWORD)
ftp.cwd(REMOTE_DIRECTORY)
renamed_file = "%s.%s" % (REMOTE_FILE, VERSION)
ftp.rename(REMOTE_FILE, renamed_file)
upload_file = open(REMOTE_FILE, "r")
command = "STOR %s" % (REMOTE_FILE)
ftp.storlines(command, upload_file)
upload_file.close()
ftp.quit()