Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import os, csv, ftplib
- folder_path="local_path"
- csvfile="output.txt"
- ftp_server="hostname"
- ftp_username="username"
- ftp_password="password"
- ftp_directory="remote_path"
- def csvOldName(file):
- old_name_split = file.split('.')
- old_name = old_name_split[0] + '_old.' + old_name_split[1]
- return old_name
- def refreshFolders(path, csvfile):
- file = open(csvfile, 'w+')
- files = [0]
- sub = "."
- list=os.listdir(path)
- foldernum = -1
- for fname in list:
- if os.path.isdir(folder_path + fname):
- foldernum += 1
- file.write(fname + "," + str(os.path.getmtime(folder_path + fname)) + "\n")
- file.close()
- def checkFTPFolders(server, username, password, path, csvfile):
- ftp = ftplib.FTP()
- ftp.connect(server)
- ftp.login(username, password)
- cmd = "RETR " + csvfile
- f = open(csvOldName(csvfile),"wb")
- ftp.retrbinary(cmd,f.write)
- f.close()
- reader = csv.reader(open(csvfile, "r"))
- print "New values"
- for row in reader:
- print row
- if float(row[1]) > os.path.getmtime(path + csvOldName(csvfile)):
- print "Folder <" + row[0] + "> was recently updated/created."
- reader = csv.reader(open(csvOldName(csvfile), "r"))
- print "Old values"
- for row in reader:
- print row
- if os.path.exists(path + row[0]) and os.path.getmtime(path + row[0]) > float(row[1]):
- print "Folder <" + row[0] + "> was recently updated."
- ftp.
- elif os.path.exists(path + row[0]) and os.path.getmtime(path + row[0]) < float(row[1]):
- print "Folder <" + row[0] + "> is outdated."
- elif os.path.exists(path + row[0]) and os.path.getmtime(path + row[0]) == float(row[1]):
- print "Folder <" + row[0] + "> is current."
- else:
- print "Folder <" + row[0] + "> was recently deleted."
- ftp.delete(csvfile)
- cmd = "STOR " + csvfile
- upload_file = open(path + csvfile)
- ftp.storbinary(cmd, upload_file)
- ftp.quit()
- refreshFolders(folder_path, csvfile)
- checkFTPFolders(ftp_server, ftp_username, ftp_password, folder_path, csvfile)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement