How do I parse a listing of files to get just the filenames in Python?

前端 未结 7 1521
一向
一向 2021-02-10 23:50

So lets say I\'m using Python\'s ftplib to retrieve a list of log files from an FTP server. How would I parse that list of files to get just the file names (the last column) ins

相关标签:
7条回答
  • 2021-02-11 00:40

    And a slightly less-optimal method, by the way, if you're stuck using retrlines() for some reason, is to pass a function as the second argument to retrlines(); it'll be called for each item in the list. So something like this (assuming you have an FTP object named 'ftp') would work as well:

    filenames = []
    ftp.retrlines('LIST', lambda line: filenames.append(line.split()[-1]))
    

    The list 'filenames' will then be a list of the file names.

    0 讨论(0)
提交回复
热议问题