如何解析文件列表以仅获取Python中的文件名? [英] How do I parse a listing of files to get just the filenames in Python?
问题描述
所以可以说我正在使用Python的 ftplib 从FTP服务器检索日志文件列表.我将如何解析该文件列表以仅获取列表中的文件名(最后一列)?有关示例输出,请参见上面的链接.
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) inside a list? See the link above for example output.
推荐答案
在这里使用retrlines()可能不是最好的主意,因为它只是打印到控制台上,因此您甚至必须做一些棘手的事情才能得到在那个输出.更好的选择是使用nlst()方法,该方法将完全返回您想要的内容:文件名列表.
Using retrlines() probably isn't the best idea there, since it just prints to the console and so you'd have to do tricky things to even get at that output. A likely better bet would be to use the nlst() method, which returns exactly what you want: a list of the file names.
这篇关于如何解析文件列表以仅获取Python中的文件名?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!