FTP file creation date

Ron Beitel ronb at not.here
Thu Oct 28 22:17:14 CEST 2004


To test for the latest version of a file on the ftpserver, 
use the ftplib.sendcmd() method with the '4-character'
ftp command for modtime.

import ftplib

ftp = ftplib.FTP("ftp.mysite.org")
f.login("anonymous", "my at email.com")

# MDTM is an abbreviated '4-char' ftp command
modtime = f.sendcmd("MDTM file.name")

# Strip off the 'reply code'
if modtime[:3] = "213":
    modtime = modtime[3:].strip()

Refer to RFC 959 - File Transfer Protocal 

Section4.1 lists the abbreviated ftp commands,
however, 'SIZE' and 'MDTM' are not covered in the RFC
Section4.2.1 covers the Reply Codes.

For examples on using f.sendcmd(), check the module's 
source code /usr/lib/python2.2/ftplib.py
Especially, look at the definitions for
	def size()
	def nlst()

If you're not concerned with portability, you could modify
your systems copy of the library.  Add....

def modtime(self, filename):
    '''Retrieve the modtime of a file.'''
    resp = self.sendcmd('MDTM ' + filename)
    if resp[:3] == '213'
        s = resp[3:].strip()
    return s

Then in your program, you can just say
t = f.modtime("file.name")

Ron Beitel

More information about the Python-list mailing list