python 2.7 - Using OS.walk and paramiko to move files to ftp server causing EOFError? -
my goal walk through files in folder (and subfolders), check whether file exists on ftp. if file doesn't exist, put in in destination folder , if exist, archive old file using rename , put new file in it's place. code far follows.
path = 'z:\\_magento images\\2014\\jun2014\\09jun2014' ssh = paramiko.sshclient() log_file = 'c:\\temp\\log.txt' paramiko.util.log_to_file(log_file) ssh.set_missing_host_key_policy(paramiko.autoaddpolicy()) def upload_images(): #' #in case server's key unknown,' #we adding automatically list of known hosts #ssh.load_host_keys(os.path.expanduser(os.path.join("~", ".ssh", "known_hosts"))) #loads user's local known host file ssh.connect('xxxxxxxxxxxxx', port=22, username='xxxxxxxx', password='xxxxxxxxx') ssh_stdin, ssh_stdout, ssh_stderr = ssh.exec_command('ls /tmp') print "output", ssh_stdout.read() #reading output of executed co'mmand error = ssh_stderr.read() #reading error stream of executed command print "err", error, len(error) #transfering files , remote machine' sftp = ssh.open_sftp() #'sftp.get(remote_path, local_path)' root, dirs, files in os.walk(path): fn in files: ftp_path = '/productimages/' + fn archive = '/productimages/archive/' + fn source = root + '\\' + fn try: sftp.stat(ftp_path) except ioerror e: print e.errno print errno.enoent if e.errno == errno.enoent: print 'this if' sftp.put(source, ftp_path) else: print 'this else' sftp.rename(ftp_path,archive) sftp.put(root + '\\' + fn, ftp_path) finally: sftp.close() ssh.close() #update_log()
what happens. if file doesn't exist eoferror. i'll have set conditions situation if file archived cross bridge when it. being thick , can't tease out issues. thoughts appreciated.
the eoferror
occurs because of misplaced finally
clause: sftp
, ssh
closed after first processed file.
the second problem sftp.rename(ftp_path,archive)
fail if archive
file exists.
the partially fixed upload_images()
may this:
def upload_images(): # code skipped def rexists(path): try: sftp.stat(path) except ioerror e: if e.errno == errno.enoent: return false raise return true try: root, dirs, files in os.walk(path): fn in files: print 'fn', fn ftp_path = '/nethome/auto.tester/tmp/productimages/' + fn archive = '/nethome/auto.tester/tmp/productimages/archive/' + fn source = root + '\\' + fn if not rexists(ftp_path): sftp.put(source, ftp_path) else: if rexists(archive): sftp.remove(archive) sftp.rename(ftp_path, archive) sftp.put(root + '\\' + fn, ftp_path) finally: sftp.close() ssh.close()
however still fails if path
contains sub-directories files.
Comments
Post a Comment