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).
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 FTP
from dateutil import parser
# ... (connection to FTP)
timestamp = ftp.voidcmd("MDTM /remote/path/file.txt")[4:].strip()
time = parser.parse(timestamp)
print(time)
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.
MLSD
modify
factYYYYMMDDHHMMSS[.sss]
For details, refer to RFC 3659 again, particularly the:
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.
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))