Get the latest FTP folder name in Python(在 Python 中获取最新的 FTP 文件夹名称)
问题描述
我正在尝试编写一个脚本来从最新的子目录中获取最新的文件Python中的FTP服务器目录.我的问题是我无法弄清楚最新的子目录.有两个选项可用,子目录有 ctime 可用.在目录名称中还提到了创建目录的日期.但我不知道如何获取最新目录的名称.我想出了以下方法(希望服务器端按最新的ctime排序).如果第一个对象是最新目录,我已经按照以下方式完成了它.
I am trying to write a script to get the latest file from the latest sub- directory of FTP server in Python. My problem is I am unable to figure out the latest sub-directory. There are two options available, sub-directories have ctime available. Also in directory name date is mentioned that on which date directory was created. But I do not know how to get the name of the latest directory. I have figured out the following way (hoping for the server side to be sorted by latest ctime). I have done it the following way which will work if first object is the latest directory.
import ftplib
import os
import time
ftp = ftplib.FTP('test.rebex.net','demo', 'password')
ftp.cwd(str((ftp.nlst())[0])) #if directory is sorted in descending order by date.
但是有什么方法可以通过 ctime 或目录名称中的日期找到确切的目录?
But is there any way where I will find the exact directory by ctime or by date in directory name ?
非常感谢各位.
推荐答案
如果你的 FTP 服务器支持 MLSD
命令,解决方案很简单:
If your FTP server supports MLSD
command, a solution is easy:
如果您想根据修改时间戳做出决定:
If you want to base the decision on a modification timestamp:
entries = list(ftp.mlsd())
# Only interested in directories
entries = [entry for entry in entries if entry[1]["type"] == "dir"]
# Sort by timestamp
entries.sort(key = lambda entry: entry[1]['modify'], reverse = True)
# Pick the first one
latest_name = entries[0][0]
print(latest_name)
如果要使用文件名:
If you want to use a file name:
# Sort by filename
entries.sort(key = lambda entry: entry[0], reverse = True)
如果您需要依赖过时的 LIST
命令,则必须解析它返回的专有列表.
If you need to rely on an obsolete LIST
command, you have to parse a proprietary listing it returns.
一个常见的 *nix 列表如下:
A common *nix listing is like:
drw-r--r-- 1 user group 4096 Mar 26 2018 folder1-20180326
drw-r--r-- 1 user group 4096 Jun 18 11:21 folder2-20180618
-rw-r--r-- 1 user group 4467 Mar 27 2018 file-20180327.zip
-rw-r--r-- 1 user group 124529 Jun 18 15:31 file-20180618.zip
有了这样的清单,这段代码就可以了:
With a listing like this, this code will do:
如果您想根据修改时间戳做出决定:
If you want to base the decision on a modification timestamp:
lines = []
ftp.dir("", lines.append)
latest_time = None
latest_name = None
for line in lines:
tokens = line.split(maxsplit = 9)
# Only interested in directories
if tokens[0][0] == "d":
time_str = tokens[5] + " " + tokens[6] + " " + tokens[7]
time = parser.parse(time_str)
if (latest_time is None) or (time > latest_time):
latest_name = tokens[8]
latest_time = time
print(latest_name)
如果要使用文件名:
If you want to use a file name:
lines = []
ftp.dir("", lines.append)
latest_name = None
for line in lines:
tokens = line.split(maxsplit = 9)
# Only interested in directories
if tokens[0][0] == "d":
name = tokens[8]
if (latest_name is None) or (name > latest_name):
latest_name = name
print(latest_name)
某些 FTP 服务器可能会在 LIST
结果中返回 .
和 ..
条目.您可能需要过滤这些内容.
Some FTP servers may return .
and ..
entries in LIST
results. You may need to filter those.
部分基于:Python FTP 按日期获取最新文件.
如果文件夹不包含任何文件,只有子文件夹,还有其他更简单的选项.
If the folder does not contain any files, only subfolders, there are other easier options.
如果你想根据修改时间戳做出决定,并且服务器支持非标准的
-t
开关,你可以使用:
lines = ftp.nlst("-t")
latest_name = lines[-1]
参见如何获取FTP文件夹中按修改时间排序的文件
如果要使用文件名:
lines = ftp.nlst()
latest_name = max(lines)
这篇关于在 Python 中获取最新的 FTP 文件夹名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:在 Python 中获取最新的 FTP 文件夹名称
基础教程推荐
- 线程时出现 msgbox 错误,GUI 块 2022-01-01
- 如何让 python 脚本监听来自另一个脚本的输入 2022-01-01
- 在 Python 中,如果我在一个“with"中返回.块,文件还会关闭吗? 2022-01-01
- Python kivy 入口点 inflateRest2 无法定位 libpng16-16.dll 2022-01-01
- 如何在海运重新绘制中自定义标题和y标签 2022-01-01
- 何时使用 os.name、sys.platform 或 platform.system? 2022-01-01
- 用于分类数据的跳跃记号标签 2022-01-01
- 筛选NumPy数组 2022-01-01
- Dask.array.套用_沿_轴:由于额外的元素([1]),使用dask.array的每一行作为另一个函数的输入失败 2022-01-01
- 使用PyInstaller后在Windows中打开可执行文件时出错 2022-01-01