ftp: get list of files

Paul Watson pwatson at redlinepy.com
Thu Feb 9 16:48:13 EST 2006


Lawrence Oluyede wrote:
> "eels" <eels at nikocity.de> writes:
> 
> 
>>With yyy = ftp.retrlines('LIST') I get this listing at stdout, but I
>>need this information at variable yyy.
>>How can I resolve this problem?
> 
> 
> As written in the doc retrlines has an optional parameter (a callback function)
> called on each line retrieved, so you can do something like this:
> 
> from ftplib import FTP
> 
> def writeline(data):
>     fd.write(data + "\n")
> 
> f = FTP('ftp.kernel.org')
> f.login()
> 
> f.cwd('/pub/linux/kernel')
> fd = open('README', 'wt')
> f.retrlines('RETR README', writeline)
> fd.close()
> f.quit()

How about a little more portability?

def writeline(data):
     fd.write(data + os.linesep)



More information about the Python-list mailing list