如何以记录时间戳的方式从 Python 中的 FTP 下载文件?我想根据服务器时间戳记录文件的修改/上次更新时间,并记录获取文件的时间。如何才能做到这一点?谢谢
user248237
问问题
1472 次
1 回答
3
我认为这就是你要找的:
import os
import ftplib
import datetime
local = 'LOCATION/WHERE/YOU/WANT/TO/DOWNLOAD'
time_stamps = {}
with ftplib.FTP('ftp.your.host') as server:
server.login(user='username', passwd='password')
for name, params in server.mlsd():
if name != '.' and name != '..':
with open(os.path.join(local, name), 'w+b') as download:
try:
server.retrbinary('RETR ' + name, download.write)
dt = params['modify']
time_stamps[name] = {
'modified': '{yy}-{mo}-{dd} {hh}:{mm}:{ss}.{ms}'.format(
yy = dt[:4],
mo = dt[4:6],
dd = dt[6:8],
hh = dt[8:10],
mm = dt[10:12],
ss = dt[12:14],
ms = dt[14:]
),
'fetched': str(datetime.datetime.now())
}
except ftplib.error_perm:
pass
print(time_stamps)
于 2013-07-13T23:00:04.073 回答