FTP module file parsing..

sismex01 at hebmex.com sismex01 at hebmex.com
Thu Dec 5 12:09:05 EST 2002


> From: Wayne Ringling [mailto:wayne at tbnets.com]
> Sent: Thursday, December 05, 2002 10:25 AM
> 
>  I want to parse a file as I down load it from a server. So I 
> do the following.
> 
> from ftplib import FTP
> ftpserver = FTP('server', 'user', 'passwd')
> ftpserver.cwd('/where the file is')
> datalist = ftpserver.retrlines('RETR filename')
> ftpserver.quit()
> 
> this dumps the whole file into the datalist.  I would like to 
> only parse the file line by line and not load the whole thing 
> into the list and also not download it to the machine and 
> then parse it.  Am I crazy for thinking this way or am I 
> missing something?
> 
> Wayne
>

Hmmm... I don't know which version of Python you're using,
but .retrlines() only returns the response line from the
server, not the list of lines retrieved.

This method takes as second argument a callback, which is
called for each line retrieved.  So in your example,
you should do something like:

>>> datalist = []
>>> ftpserver.retrlines("RETR filename", datalist.append)

This way, all the lines will be appended to the list you
provided.  BUT, if you want to actually check each line,
then you can define a function which takes a single
argument and use that function in place of "datalist.append".

Hope this helps.

-gustavo




More information about the Python-list mailing list