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

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


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:

A simple example for MDTM:

from ftplib import FTPfrom 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:

from ftplib import FTPfrom 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 Gregoriancalendar regardless of what calendar may have been in use at the dateand 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 FTPfrom 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.


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