[Webinar] Streamline your web hosting managementRegister Today

x
?
Solved

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

Posted on 2007-08-01
3
Medium Priority
?
4,352 Views
Last Modified: 2013-12-02
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)
0
Comment
Question by:mfcnewbie
  • 2
3 Comments
 
LVL 25

Accepted Solution

by:
clockwatcher earned 1000 total points
ID: 19614865
Use the raw ftp MDTM command to get the file modification time and then use os.utime to modify the file attributes of the file you created.

import os
import time
import datetime
import ftplib
ftp = ftplib.FTP('ftp.microsoft.com')
ftp.login()
fname="readme.txt"
ftp.cwd("developr")
ftp.retrbinary("RETR %s" % fname, open(fname,"wb").write)
resp=ftp.sendcmd("MDTM %s" % fname)
if resp[0] == '2':
    timestamp = time.mktime(datetime.datetime.strptime(resp[4:18],"%Y%m%d%H%M%S").timetuple())
    os.utime(fname, (timestamp, timestamp))
0
 

Author Comment

by:mfcnewbie
ID: 19626637
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.

0
 
LVL 25

Expert Comment

by:clockwatcher
ID: 19629577
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())
0

Featured Post

Learn to develop an Android App

Want to increase your earning potential in 2018? Pad your resume with app building experience. Learn how with this hands-on course.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Here I am using Python IDLE(GUI) to write a simple program and save it, so that we can just execute it in future. Because when we write any program and exit from Python then program that we have written will be lost. So for not losing our program we…
Sequence is something that used to store data in it in very simple words. Let us just create a list first. To create a list first of all we need to give a name to our list which I have taken as “COURSE” followed by equals sign and finally enclosed …
Learn the basics of modules and packages in Python. Every Python file is a module, ending in the suffix: .py: Modules are a collection of functions and variables.: Packages are a collection of modules.: Module functions and variables are accessed us…
Learn the basics of while and for loops in Python.  while loops are used for testing while, or until, a condition is met: The structure of a while loop is as follows:     while <condition>:         do something         repeate: The break statement m…
Suggested Courses

591 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question