How to get FTP file's modify time using Python ftplib

后端 未结 2 1715
北恋
北恋 2020-11-27 20:24

I\'m trying to load a CSV file to Amazon S3 with Python. I need to know CSV file\'s modification time. I\'m using ftplib to connect FTP with Python (2.7).

相关标签:
2条回答
  • 2020-11-27 20:52

    When I want to change the file modification time, I use an FTP client on the console. Log on to a remote FTP ftp ftp.dic.com

    • cd commands go to the correct directory
    • SITE command to move the extended command mode
    • UTIME somefile.txt 20050101123000 20050101123000 20050101123000 UTC

    change the access time, modification time, it's time to create a directory on 2005-01-01 12:30:00 somefile.txt

    Complete example:

    site UTIME somefile.txt 20150331122000 20150331122000 20150331122000 UTC

    Please feel free to sit back and wish you a pleasant journey in time :)

    0 讨论(0)
  • 2020-11-27 21:01

    MLST or MDTM

    While you can retrieve a timestamp of an individual file over FTP with MLST or MDTM commands, neither is supported by ftplib.

    Of course you can implement the MLST or MDTM on your own using FTP.voidcmd.

    For details, refer to RFC 3659, particularly the:

    • 3. File Modification Time (MDTM)
    • 7. Listings for Machine Processing (MLST and MLSD)

    A simple example for MDTM:

    from ftplib import FTP
    from dateutil import parser
    
    # ... (connection to FTP)
    
    timestamp = ftp.voidcmd("MDTM /remote/path/file.txt")[4:].strip()
    
    time = parser.parse(timestamp)
    
    print(time)
    

    MLSD

    The only command explicitly supported by the ftplib library that can return standardized file timestamp is MLSD via FTP.mlsd method. Though its use makes sense only if you want to retrieve timestamps for more files.

    • Retrieve a complete directory listing using MLSD
    • Search the returned collection for the file(s) you want
    • Retrieve modify fact
    • Parse it according to the specification, YYYYMMDDHHMMSS[.sss]

    For details, refer to RFC 3659 again, particularly the:

    • 7.5.3. The modify Fact section
    • 2.3. Times section
    from ftplib import FTP
    from dateutil import parser
    
    # ... (connection to FTP)
    
    files = ftp.mlsd("/remote/path")
    
    for file in files:
        name = file[0]
        timestamp = file[1]['modify']
        time = parser.parse(timestamp)
        print(name + ' - ' + str(time))
    

    Note that times returned by MLST, MLSD and MDTM are in UTC (unless the server is broken). So you may need to correct them for your local timezone.

    Again, refer to RFC 3659 2.3. Times section:

    Time values are always represented in UTC (GMT), and in the Gregorian calendar regardless of what calendar may have been in use at the date and time indicated at the location of the server-PI.


    LIST

    If the FTP server does not support any of MLST, MLSD and MDTM, all you can do is to use an obsolete LIST command. That involves parsing a proprietary listing it returns.

    A common *nix listing is like:

    -rw-r--r-- 1 user group           4467 Mar 27  2018 file1.zip
    -rw-r--r-- 1 user group         124529 Jun 18 15:31 file2.zip
    

    With a listing like this, this code will do:

    from ftplib import FTP
    from dateutil import parser
    
    # ... (connection to FTP)
    
    lines = []
    ftp.dir("/remote/path", lines.append)
    
    for line in lines:
        tokens = line.split(maxsplit = 9)
        name = tokens[8]
        time_str = tokens[5] + " " + tokens[6] + " " + tokens[7]
        time = parser.parse(time_str)
        print(name + ' - ' + str(time))
    

    Finding the latest file

    See also Python FTP get the most recent file by date.

    0 讨论(0)
提交回复
热议问题