Get the latest FTP folder name in Python

前端 未结 1 410
抹茶落季
抹茶落季 2020-12-20 04:37

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-directo

相关标签:
1条回答
  • 2020-12-20 05:30

    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)
      

    If you need to rely on an obsolete LIST command, you have to parse a proprietary listing it returns.

    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)
      

    Some FTP servers may return . and .. entries in LIST results. You may need to filter those.


    Partially based on: Python FTP get the most recent file by date.


    If the folder does not contain any files, only subfolders, there are other easier options.

    • If you want to base the decision on a modification timestamp and the server supports non-standard -t switch, you can use:

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

      See How to get files in FTP folder sorted by modification time

    • If you want to use a file name:

      lines = ftp.nlst()
      latest_name = max(lines)
      
    0 讨论(0)
提交回复
热议问题