Python FTP 按日期获取最新文件
我正在使用 ftplib 连接到 ftp 站点。我想获取最近上传的文件并下载它。我能够连接到 ftp 服务器并列出文件,我还将它们放入列表中并转换了 datefield
。是否有任何函数/模块可以获取最近的日期并从列表中输出整行?
#!/usr/bin/env python
import ftplib
import os
import socket
import sys
HOST = 'test'
def main():
try:
f = ftplib.FTP(HOST)
except (socket.error, socket.gaierror), e:
print 'cannot reach to %s' % HOST
return
print "Connect to ftp server"
try:
f.login('anonymous','[email protected]')
except ftplib.error_perm:
print 'cannot login anonymously'
f.quit()
return
print "logged on to the ftp server"
data = []
f.dir(data.append)
for line in data:
datestr = ' '.join(line.split()[0:2])
orig-date = time.strptime(datestr, '%d-%m-%y %H:%M%p')
f.quit()
return
if __name__ == '__main__':
main()
已解决:
data = []
f.dir(data.append)
datelist = []
filelist = []
for line in data:
col = line.split()
datestr = ' '.join(line.split()[0:2])
date = time.strptime(datestr, '%m-%d-%y %H:%M%p')
datelist.append(date)
filelist.append(col[3])
combo = zip(datelist,filelist)
who = dict(combo)
for key in sorted(who.iterkeys(), reverse=True):
print "%s: %s" % (key,who[key])
filename = who[key]
print "file to download is %s" % filename
try:
f.retrbinary('RETR %s' % filename, open(filename, 'wb').write)
except ftplib.err_perm:
print "Error: cannot read file %s" % filename
os.unlink(filename)
else:
print "***Downloaded*** %s " % filename
return
f.quit()
return
一个问题,是否可以从字典中检索第一个元素?我在这里所做的是 for 循环仅运行一次并退出,从而给我第一个排序值,这很好,但我不认为以这种方式执行此操作是一个好习惯。
I am using ftplib to connect to an ftp site. I want to get the most recently uploaded file and download it. I am able to connect to the ftp server and list the files, I also have put them in a list and got the datefield
converted. Is there any function/module which can get the recent date and output the whole line from the list?
#!/usr/bin/env python
import ftplib
import os
import socket
import sys
HOST = 'test'
def main():
try:
f = ftplib.FTP(HOST)
except (socket.error, socket.gaierror), e:
print 'cannot reach to %s' % HOST
return
print "Connect to ftp server"
try:
f.login('anonymous','[email protected]')
except ftplib.error_perm:
print 'cannot login anonymously'
f.quit()
return
print "logged on to the ftp server"
data = []
f.dir(data.append)
for line in data:
datestr = ' '.join(line.split()[0:2])
orig-date = time.strptime(datestr, '%d-%m-%y %H:%M%p')
f.quit()
return
if __name__ == '__main__':
main()
RESOLVED:
data = []
f.dir(data.append)
datelist = []
filelist = []
for line in data:
col = line.split()
datestr = ' '.join(line.split()[0:2])
date = time.strptime(datestr, '%m-%d-%y %H:%M%p')
datelist.append(date)
filelist.append(col[3])
combo = zip(datelist,filelist)
who = dict(combo)
for key in sorted(who.iterkeys(), reverse=True):
print "%s: %s" % (key,who[key])
filename = who[key]
print "file to download is %s" % filename
try:
f.retrbinary('RETR %s' % filename, open(filename, 'wb').write)
except ftplib.err_perm:
print "Error: cannot read file %s" % filename
os.unlink(filename)
else:
print "***Downloaded*** %s " % filename
return
f.quit()
return
One problem, is it possible to retrieve the first element from the dictionary? what I did here is that the for loop runs only once and exits thereby giving me the first sorted value which is fine, but I don't think it is a good practice to do it in this way..
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
对于那些寻找在文件夹中查找最新文件的完整解决方案的人:
MLSD
如果您的 FTP 服务器支持 MLSD 命令,解决方案很简单:
LIST
如果您需要依赖过时的 LIST 命令,您必须解析它返回的专有列表。
常见的 *nix 列表如下:
有了这样的列表,这段代码就可以了:
这是一种相当脆弱的方法。
MDTM
一种更可靠但效率较低的方法是使用 MDTM 命令来检索单个文件/文件夹的时间戳:
有关代码的替代版本,请参阅 @Paulo 的回答。
非标准 -t 开关
某些 FTP 服务器支持专有的非标准
-t
用于NLST
(或LIST
)命令的开关。请参阅如何获取按修改时间排序的 FTP 文件夹中的文件。
下载找到的文件
无论您使用什么方法,一旦获得了
latest_name
,您就可以像下载任何其他文件一样下载它:另请参阅
For those looking for a full solution for finding the latest file in a folder:
MLSD
If your FTP server supports
MLSD
command, a solution is easy:LIST
If you need to rely on an obsolete
LIST
command, you have to parse a proprietary listing it returns.Common *nix listing is like:
With a listing like this, this code will do:
This is a rather fragile approach.
MDTM
A more reliable, but a way less efficient, is to use
MDTM
command to retrieve timestamps of individual files/folders:For an alternative version of the code, see the answer by @Paulo.
Non-standard -t switch
Some FTP servers support a proprietary non-standard
-t
switch forNLST
(orLIST
) command.See How to get files in FTP folder sorted by modification time.
Downloading found file
No matter what approach you use, once you have the
latest_name
, you download it as any other file:See also
为什么不使用下一个目录选项?
使用此选项,文件列表按时间从最新到最旧的顺序排列。然后只需检索列表中的第一个文件即可下载。
Why don't you use next dir option?
With this option the file listing is time ordered from newest to oldest. Then just retrieve the first file in the list to download it.
使用
NLST
,如 Martin Prikryl 的回复所示,你应该使用
sorted
方法:With
NLST
, like shown in Martin Prikryl's response,you should use
sorted
method:如果您有
time.struct_time
(strptime
会给你这个)在一个列表中那么你所要做的就是对列表进行排序
。这是一个例子:
If you have all the dates in
time.struct_time
(strptime
will give you this) in a list then all you have to do issort
the list.Here's an example :
我不知道你的 ftp 怎么样,但你的例子不适合我。我更改了与日期排序部分相关的一些行:
I don't know how it's your ftp, but your example was not working for me. I changed some lines related to the date sorting part: