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

Posted on 2007-08-01
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

import ftplib
ftp = ftplib.FTP('..')
data = []
for f in data:
        y=ftp.retrbinary("RETR %s" % fname, open('tmp1/%s' % fname,"wb").write)
Question by:mfcnewbie
    LVL 25

    Accepted Solution

    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.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))

    Author Comment

    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.

    LVL 25

    Expert Comment

    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())

    Featured Post

    Courses: Start Training Online With Pros, Today

    Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

    Join & Write a Comment

    Introduction On September 29, 2012, the Python 3.3.0 was released; nothing extremely unexpected,  yet another, better version of Python. But, if you work in Microsoft Windows, you should notice that the Python Launcher for Windows was introduced wi…
    The purpose of this article is to demonstrate how we can upgrade Python from version 2.7.6 to Python 2.7.10 on the Linux Mint operating system. I am using an Oracle Virtual Box where I have installed Linux Mint operating system version 17.2. Once yo…
    Learn the basics of strings in Python: declaration, operations, indices, and slicing. Strings are declared with quotations; for example: s = "string": Strings are immutable.: Strings may be concatenated or multiplied using the addition and multiplic…
    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…

    754 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

    Need Help in Real-Time?

    Connect with top rated Experts

    23 Experts available now in Live!

    Get 1:1 Help Now