Advertisement
Guest User

Untitled

a guest
Jun 7th, 2017
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.92 KB | None | 0 0
  1. import os, csv, ftplib
  2.  
  3. folder_path="local_path"
  4. csvfile="output.txt"
  5. ftp_server="hostname"
  6. ftp_username="username"
  7. ftp_password="password"
  8. ftp_directory="remote_path"
  9.  
  10. def csvOldName(file):
  11.     old_name_split = file.split('.')
  12.     old_name = old_name_split[0] + '_old.' + old_name_split[1]
  13.     return old_name
  14.  
  15. def refreshFolders(path, csvfile):
  16.     file = open(csvfile, 'w+')
  17.     files = [0]
  18.     sub = "."
  19.     list=os.listdir(path)
  20.     foldernum = -1
  21.     for fname in list:
  22.         if os.path.isdir(folder_path + fname):
  23.             foldernum += 1
  24.             file.write(fname + "," + str(os.path.getmtime(folder_path + fname)) + "\n")
  25.     file.close()
  26.  
  27. def checkFTPFolders(server, username, password, path, csvfile):
  28.     ftp = ftplib.FTP()
  29.     ftp.connect(server)
  30.     ftp.login(username, password)
  31.     cmd = "RETR " + csvfile
  32.     f = open(csvOldName(csvfile),"wb")
  33.     ftp.retrbinary(cmd,f.write)
  34.     f.close()
  35.     reader = csv.reader(open(csvfile, "r"))
  36.     print "New values"
  37.     for row in reader:
  38.         print row
  39.         if float(row[1]) > os.path.getmtime(path + csvOldName(csvfile)):
  40.             print "Folder <" + row[0] + "> was recently updated/created."
  41.     reader = csv.reader(open(csvOldName(csvfile), "r"))
  42.     print "Old values"
  43.     for row in reader:
  44.         print row
  45.         if os.path.exists(path + row[0]) and os.path.getmtime(path + row[0]) > float(row[1]):
  46.             print "Folder <" + row[0] + "> was recently updated."
  47.             ftp.
  48.         elif os.path.exists(path + row[0]) and os.path.getmtime(path + row[0]) < float(row[1]):
  49.             print "Folder <" + row[0] + "> is outdated."
  50.         elif os.path.exists(path + row[0]) and os.path.getmtime(path + row[0]) == float(row[1]):
  51.             print "Folder <" + row[0] + "> is current."
  52.         else:
  53.             print "Folder <" + row[0] + "> was recently deleted."
  54.     ftp.delete(csvfile)
  55.     cmd = "STOR " + csvfile
  56.     upload_file = open(path + csvfile)
  57.     ftp.storbinary(cmd, upload_file)
  58.     ftp.quit()
  59.  
  60. refreshFolders(folder_path, csvfile)
  61. checkFTPFolders(ftp_server, ftp_username, ftp_password, folder_path, csvfile)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement