[python-win32] capturing stdout output from ftplib

Andrew Bennetts andrew-pywin32@puzzling.org
Thu, 13 Jun 2002 15:01:00 +1000


On Wed, Jun 12, 2002 at 11:44:11PM -0500, David Rock wrote:
> I would like to capture the output from ftp.dir() or ftp.retrlines( 'LIST' ),
> both of which output to stdout by default. Preferably, I would like this
> collected as a list of strings that I could split to get the filenames for
> comparison against an existing filename.
> 
> I have found references to using a callback function as an argument, but
> I don't know how this would work, either.

>From the ftplib documentation:
"""The callback function is called for each line, with the trailing CRLF
stripped ..."""

So,

    fileList = []
    ftp.retrlines('LIST', fileList.append)

That should call fileList.append for each line returned by the LIST
command, resulting in the list you're interested in.  You should be able
to do that ftp.dir, too.

-Andrew.