【问题标题】:Get the latest FTP folder name in Python在 Python 中获取最新的 FTP 文件夹名称
【发布时间】:2019-01-16 07:23:55
【问题描述】:

我正在尝试编写一个脚本来从最新的子目录中获取最新的文件 Python中的FTP服务器目录。我的问题是我无法弄清楚 最新的子目录。有两个选项可用,子目录有 ctime 可用。在目录名称中还提到了创建目录的日期。但我不知道如何获取最新目录的名称。我想出了以下方法(希望服务器端按最新的ctime排序)。如果第一个对象是最新目录,我已经按照以下方式完成了它。

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 或目录名称中的日期找到确切的目录?

非常感谢各位。

【问题讨论】:

    标签: python ftp ftplib


    【解决方案1】:

    如果你的 FTP 服务器支持MLSD 命令,解决方法很简单:

    • 如果您想根据修改时间戳做出决定:

      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)
      
    • 如果要使用文件名:

      # Sort by filename
      entries.sort(key = lambda entry: entry[0], reverse = True)
      

    如果您需要依赖过时的LIST 命令,则必须解析它返回的专有列表。

    一个常见的 *nix 列表如下:

    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
    

    有了这样的清单,这段代码就可以了:

    • 如果您想根据修改时间戳做出决定:

      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)
      
    • 如果要使用文件名:

      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 结果中返回 ... 条目。您可能需要过滤这些内容。


    部分基于:Python FTP get the most recent file by date


    如果文件夹不包含任何文件,只有子文件夹,还有其他更简单的选项。

    • 如果您想根据修改时间戳做出决定并且服务器支持非标准的-t开关,您可以使用:

      lines = ftp.nlst("-t")
      latest_name = lines[-1]
      

      How to get files in FTP folder sorted by modification time

    • 如果要使用文件名:

      lines = ftp.nlst()
      latest_name = max(lines)
      

    【讨论】:

    • 非常感谢我的朋友。你是一个真正的宝石 :) 我是一个初学者(1 个月大的蟒蛇宝宝),你的解决方案真的帮助我理解它。
    • 我该怎么做那个朋友?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-27
    • 1970-01-01
    • 2018-08-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多