Created
December 26, 2015 19:16
-
-
Save controversial/d43e11857a875d187a2a to your computer and use it in GitHub Desktop.
FTP.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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