Skip to content

Instantly share code, notes, and snippets.

@controversial
Created December 26, 2015 19:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save controversial/d43e11857a875d187a2a to your computer and use it in GitHub Desktop.
Save controversial/d43e11857a875d187a2a to your computer and use it in GitHub Desktop.
FTP.py
import ftplib, os, shutil, tempfile, time
ftp=ftplib.FTP('ftp.deentaylor.com')
print ftp.login('pythonista@deentaylor.com', 'FTPythonista')
doc_path = os.path.expanduser('~/Documents/')
filename=time.strftime("%Y-%m-%d&%H:%M:%S")
backup_path = filename+'.zip'
if os.path.exists(backup_path):
os.remove(backup_path)
print 'Creating archive...',
shutil.make_archive(os.path.join(tempfile.gettempdir(), filename), 'zip')
shutil.move(os.path.join(tempfile.gettempdir(), backup_path), backup_path)
print 'Done!'
print 'Uploading {} bytes to FTP server...'.format(str(os.path.getsize(backup_path))),
def upload(ftp, file):
ext= os.path.splitext(file)[1]
if ext in (".txt", ".htm", ".html"):
ftp.storlintes("STOR "+file, open(file))
else:
ftp.storbinary("STOR "+file, open(file,"rb"),1024)
try:
upload(ftp,backup_path)
print "Done!"
except:
print "Upload failed"
os.remove(backup_path)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment