ftp a file by date and name match

John Hunter jdhunter at ace.bsd.uchicago.edu
Tue Jul 8 12:57:36 EDT 2003


>>>>> "Bob" == Bob  <bobx at linuxmail.org> writes:

    Bob> I am trying to get the latest file from an ftp site that
    Bob> matches a pattern and is the latest one. I have it in Perl
    Bob> and would like to see the Python version for comparison.

Here's analogous code to filter a list of filenames using a regex in
python.  For simplicity, I'll just make up the list of filenames
rather than get them from some ftp server

  import re, sys

  files = ['20021203.somename.x86.exe',
           '20030103.somename.x86.exe',
           '20030503.somename.x86.exe',
           'README',]

  matches = [fname for fname in files if re.match('\d{8}.*x86.exe', fname)]
  matches.sort()        # sort to get the most recent last

  if len(matches)==0: 
      print 'No file matched'
      sys.exit()  # byebye

  recent = matches[-1]  # get the last element of the list

Note if you assume the file list is already ordered lexographically
and don't need to sort, and don't want to check for the no match
condition, and like tight, hard to read perl code, you could just do
everything in one line, though this is frowned upon by python fans.

 recent = [fname for fname in files if re.match('\d{8}.*x86.exe', fname)][-1]

Now we have the filename of the most recent match, and can proceed to
fetch it from the ftp server.  Here's some example code to log into an
ftp server, change dir, and fetch a file in binary mode.  Again, I'll
just make up a filename

  from ftplib import FTP
  recent = 'welcome.msg'  # an example file for testing
  ftp = FTP()   # connect to host, default port
  ftp.connect('ftp.gnu.org')
  ftp.login('ftp', 'jdhunter at ace.bsd.uchicago.edu') #user, pass
  ftp.cwd('/pub')
  # get the most recent file and store it on the local sys with same name
  ftp.retrbinary('RETR %s' % recent, file(recent, 'wb').write)

All done. All you have to do now is get a listing of files from your
favorite ftp server and glue the two pieces together.

Here are the docs for the ftplib module (part of the standard library)
http://python.org/doc/current/lib/module-ftplib.html

Perhaps you'll join the ranks of the converted?

JDH





More information about the Python-list mailing list