Link to home
Start Free TrialLog in
Avatar of mfcnewbie
mfcnewbie

asked on

How do I preserve file date and time with ftp and python?

I'm copying some files from an ftp site, to a local site, using a python script. But the default behavior has the local file with the current date-time, rather than the original date-time.

I'm getting all the files matching "abc*.csv", and copying them to a local temp directory. It works fine, but the file date-time of the original files are not getting preserved.

Any ideas:

Here's my script

#!/usr/bin/python
import ftplib
ftp = ftplib.FTP('..')
ftp.login('..')
data = []
ftp.dir('abc*.csv',data.append)
for f in data:
        y=ftp.retrbinary("RETR %s" % fname, open('tmp1/%s' % fname,"wb").write)
ASKER CERTIFIED SOLUTION
Avatar of clockwatcher
clockwatcher

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of mfcnewbie
mfcnewbie

ASKER

I took a while to acknowledge this as the solution, because it didn't seem to work.
But then I tried it on another PC, with Python 2.5 and it worked flawlessly.
Apparently Python 2.4 doesn't have strptime in the datetime.datetime class.

How would you do it with Python 2.4?

Anyways, thanks.

Just have to parse the time manually.  Replace this line:

   timestamp = time.mktime(datetime.datetime.strptime(resp[4:18],"%Y%m%d%H%M%S").timetuple())

With this one:

  timestamp = time.mktime(datetime.datetime(int(resp[4:8]),int(resp[8:10]),int(resp[10:12]),int(resp[12:14]),int(resp[14:16]),int(resp[16:18])).timetuple())