get todays files

Andrew D andrew at doadesweb.co.uk
Wed Dec 10 20:00:47 CET 2008


On Dec 10, 6:55 pm, Tim Chase <python.l... at tim.thechases.com> wrote:
> > I have a script that will login to my ftp server and download
> > all the backup files, but I want it to only download the files
> > that were created today, e.g. if I ran the script today I want
> > it to only fetch files created today.
>
> Use Python's ftp module and send the MDTM command to get back the
> timestamp of the filename.  Insecurely, this would look something
> like
>
>    from ftplib import FTP
>    hostname = "ftp.mozilla.org"
>    conn = FTP(hostname)
>    user = "anonymous"
>    password = "u... at example.com"
>    conn.login(user, password)
>    filename = "pub/README"
>    results = conn.sendcmd("MDTM %s" % filename)
>    code, stamp = results.split(None, 1)
>    assert code == "213", "Unexpected result"
>    print "%s was modified on %s" % (filename, stamp)
>    today = '20081210'
>    if stamp[:8] == today:
>      process(filename)
>    else:
>      print "ignoring", filename
>
> The MDTM command is not part of the core RFC-959, but rather the
> RFC-3659[1] so you might run across some servers that don't
> support it.  You can read more about the Python ftplib module at
> [2] which would be where you want to read up on pulling back a
> listing of the directory of file-names to check.  There is a NLST
> command (I don't have a server handy that supports this command).
>   The LIST command returns pretty/readable information that's not
> quite so machine-parsing friendly (at least in a cross-FTP-server
> sort of way).  However, that part, I leave as an exercise for the
> reader along with the complications of the "today" bit.
>
> Oh, SteveH, I checked your FTP-cloning source in my Python dir,
> and it doesn't look like it does anything regarding file-times in
> it, so that may have been a red-herring.  Unless you've added
> something since the ver. I've got here.
>
> -tkc
>
> [1]http://en.wikipedia.org/wiki/List_of_FTP_commands
>
> [2]http://www.python.org/doc/2.5.2/lib/ftp-objects.html

This looks very good and I have tested successfully, but is there a
way I can set the today to automatically become todays date in that
format?

Thanks though, this is what I was looking for!

Andrew



More information about the Python-list mailing list