1

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.

4

1 回答 1

1

默认情况下retrlines 打印到标准输出。但是,您可以使用第二个(回调)参数将输出收集到一个列表中,如下所示:

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)
于 2013-09-09T13:41:31.330 回答