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)
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Just have to parse the time manually. Replace this line:
timestamp = time.mktime(datetime.datet ime.strpti me(resp[4: 18],"%Y%m% d%H%M%S"). timetuple( ))
With this one:
timestamp = time.mktime(datetime.datet ime(int(re sp[4:8]),i nt(resp[8: 10]),int(r esp[10:12] ),int(resp [12:14]),i nt(resp[14 :16]),int( resp[16:18 ])).timetu ple())
timestamp = time.mktime(datetime.datet
With this one:
timestamp = time.mktime(datetime.datet
ASKER
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.