get todays files
Tim Chase
python.list at tim.thechases.com
Wed Dec 10 13:55:51 EST 2008
> 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 = "user 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
More information about the Python-list
mailing list