FTP file creation date

E

[ EuGeNe ]

Hi all,

I would like to write a script that downloads one file from a ftp
server if the file creation date satisfy a condition.

I can't figure out how to find from a ftp server what is the creation
date of the file (using python).

Any idea?

Thanks for your help!

EuGeNe
 
L

Larry Bates

Hi all,

I would like to write a script that downloads one file from a ftp
server if the file creation date satisfy a condition.

I can't figure out how to find from a ftp server what is the creation
date of the file (using python).

Any idea?

Thanks for your help!

EuGeNe

I don't believe there is anything like .stat methods included
in ftplib. I have always retrieved a directory of the files and
extracted the creation dates from each file manually. Then
acted on them accordingly. You should note that the dates on
most FTP servers are relative to the clock on that server (not
your local clock). If the clock on FTP server is different
from yours, either because it is off or because it is in a
different timezone, you must adjust accordingly.

Larry Bates
 
S

Steve Holden

Hi all,

I would like to write a script that downloads one file from a ftp
server if the file creation date satisfy a condition.

I can't figure out how to find from a ftp server what is the creation
date of the file (using python).

Any idea?

Thanks for your help!

EuGeNe

Take a look at the ftpmirror.py script that comes in the Tools
directory. It might give you some ideas.

regards
Steve
 
R

Ron Beitel

Eugene,

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.

#!/usr/bin/python
import ftplib

ftp = ftplib.FTP("ftp.mysite.org")
f.login("anonymous", "(e-mail address removed)")

# 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
http://www.faqs.org/rfcs/rfc959.html

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
 
G

G?nter Jantzen

Hi all,

I would like to write a script that downloads one file from a ftp
server if the file creation date satisfy a condition.

I can't figure out how to find from a ftp server what is the creation
date of the file (using python).

Any idea?

Thanks for your help!

EuGeNe

import os, ftplib, time

#==============================================================================
thisyear = time.localtime().tm_year
monthdict={'Jan': 1,'Feb': 2,'Mar': 3,'Apr': 4,'May': 5,'Jun':
6,'Jul': 7,'Aug': 8,'Sep': 9,'Oct': 10,'Nov': 11,'Dec':12}
#==============================================================================
def listdir(ftp, path = '.'):
filetimes={}
#-------------------
def callback(line):
ls = line.split()
#print ls -> ['drwxrwxr-x', '2', 'ftpclient', 'k3', '335872',
'Jul', '23', '15:31', 'trash_check']
if len(ls) == 9:
access, x, y, z, size, ls_month, ls_day, ls_union,
ls_filename = ls
tm_mon = monthdict[ls_month]
tm_mday = int(ls_day)
assert len(ls_union) in (4,5)
if len(ls_union) == 5:
assert ls_union[2] == ':'
tm_year = thisyear
tm_hour, tm_min =
int(ls_union[0:2]),int(ls_union[3:5])
elif len(ls_union) == 4:
tm_year = int(ls_union)
tm_hour, tm_min = 0, 0

ftime = time.mktime((tm_year, tm_mon, tm_mday, tm_hour,
tm_min, 0,0,0,-1))
filetimes[ls_filename] = ftime
#------------------
ftp.dir(path, callback)
return filetimes
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,901
Latest member
Noble71S45

Latest Threads

Top