Question

This simple code:

import ftplib
ftp = ftplib.FTP("ladsweb.nascom.nasa.gov")
ftp.login()
ftp.cwd("allData/5/MOD11A1/2012/193/")
a = ftp.retrlines('NLST')

Logs into an ftp, changes the current directory, and lists the names of the files contained within the current directory.

If I afterwards check what variable a contains inside, I get this:

>>> print (a)
226 Listing completed.

So, I don't understand where the list of the file names is stored. Any idea? I want to apply some regular expression operations to the file names later on.

Was it helpful?

Solution

The retrlines prints to stdout by default. You can collect the output in a list, however, by using the second (callback) argument like this:

import ftplib
ftp = ftplib.FTP("ladsweb.nascom.nasa.gov")
ftp.login()
ftp.cwd("allData/5/MOD11A1/2012/193/")
filenames = []
ftp.retrlines('NLST', filenames.append)
print(filenames)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top