get todays files
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
> 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:
> print "ignoring", filename
> The MDTM command is not part of the core RFC-959, but rather the
> RFC-3659 so you might run across some servers that don't
> support it. You can read more about the Python ftplib module at
>  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.
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
Thanks though, this is what I was looking for!
More information about the Python-list