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

Lawrence Johnston picture Lawrence Johnston · Oct 26, 2008 · Viewed 11.7k times · Source

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.

Answer

James Bennett picture James Bennett · Oct 26, 2008

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.