title: Syncing directories with python's ftplib
body: <p>I'm learning python and trying to write a code to sync two directories: one is on ftp server, the other is on my local disk. So far, I wrote a working code but I have a question or two about it :)</p>
  
  <pre><code>import os
  from ftplib import FTP
  
  h_local_files = [] # create local dir list
  h_remote_files = [] # create remote dir list
  
  h_local = 'C:\\something\\bla\\' # local dir
  
  ftp = FTP('ftp.server.com')
  ftp.login('user', 'pass')
  
  if os.listdir(h_local) == []:
      print 'LOCAL DIR IS EMPTY'
  else:
      print 'BUILDING LOCAL DIR FILE LIST...'
      for file_name in os.listdir(h_local):
          h_local_files.append(file_name) # populate local dir list
  
  ftp.sendcmd('CWD /some/ftp/directory')
  print 'BUILDING REMOTE DIR FILE LIST...\n'
  for rfile in ftp.nlst():
      if rfile.endswith('.jpg'): # i need only .jpg files
          h_remote_files.append(rfile) # populate remote dir list
  
  h_diff = sorted(list(set(h_remote_files) - set(h_local_files))) # difference between two lists
  
  for h in h_diff:
      with open(os.path.join(h_local,h), 'wb') as ftpfile:
          s = ftp.retrbinary('RETR ' + h, ftpfile.write) # retrieve file
          print 'PROCESSING', h
          if str(s).startswith('226'): # comes from ftp status: '226 Transfer complete.'
              print 'OK\n' # print 'OK' if transfer was successful
          else:
              print s # if error, print retrbinary's return
  </code></pre>
  
  <p>This piece of code should make two python lists: a list of files in local directory and a list of files in ftp directory. After removing duplicates from lists, the script should download 'missing' files to my local directory.</p>
  
  <p>For now, this piece of code is doing what I need, but I have noticed that when I run it my output is not acting how I imagine it would act :)</p>
  
  <p>For example, my current output goes:</p>
  
  <pre><code>PROCESSING 2012-01-17_07.05.jpg
  OK
  
  # LONG PAUSE HERE
  
  PROCESSING 2012-01-17_07.06.jpg
  OK
  
  # LONG PAUSE HERE
  
  PROCESSING 2012-01-17_07.06.jpg
  OK
  
  etc...
  </code></pre>
  
  <p>but I imagine that it should work like this:</p>
  
  <pre><code>PROCESSING 2012-01-17_07.05.jpg
  # LONG PAUSE HERE (WHILE DOWNLOADING)
  OK
  
  PROCESSING 2012-01-17_07.06.jpg
  # LONG PAUSE HERE (WHILE DOWNLOADING)
  OK
  
  PROCESSING 2012-01-17_07.06.jpg
  # LONG PAUSE HERE (WHILE DOWNLOADING)
  OK
  
  etc...
  </code></pre>
  
  <p>As I said, I just started to learn python, and maybe I'm doing some stuff here completely wrong (<code>if str(s).startswith('226')</code>????). Maybe I cannot achieve this with<code>ftplib</code> only? So in the end my questions are:</p>
  
  <p>What am I doing wrong here? :)<br>
  How to produce 'proper' output and is there a way to print some kind of status while downloading a file (at least a line of dots), for example:</p>
  
  <pre><code>PROCESSING 2012-01-17_07.05.jpg
  ..........
  OK
  
  PROCESSING 2012-01-17_07.06.jpg
  ......
  OK
  
  PROCESSING 2012-01-17_07.06.jpg
  ...............
  OK
  
  etc...
  </code></pre>
  
  <p>Thanks a lot for helping!</p>