Using a Callback Function - ftplib

seldan24 seldan24 at gmail.com
Mon Aug 17 13:43:33 EDT 2009


Hello,

I'm utterly confused by something which is most likely trivial.  I'm
attempting to connect to an FTP server, retrieve a list of files, and
store than in an array.  I.e.:

import ftplib

ftp = ftplib.FTP(server)
ftp.login(user, pass)
ftp.cwd(conf['testdir'])
ftp.retrlines('NLST ' + "testfile*.txt")
ftp.quit()

The above example works fine... and would return a list of any files
that match "testfile*.txt" to standard out.  The issue is I don't want
that to go to stdout, I'd rather capture them within an array so I can
retrieve them later.

If I try something like:

my_files = ftp.retrlines('NLST ' + "testfile*.txt")

Then, my_files, will just print out the return code of the FTP NLST
command.  I'm trying to get the file names themselves.  Now, I've read
through the ftplib module section of the Python documentation and it
says that, by default, the output goes to sys.stdout unless a callback
function is used.  Here is where I get utterly lost.  I can certainly
see the files being outputted to sys.stdout, but don't know how to
capture that output... i.e.

testfile1.txt
testfile2.txt
testfile3.txt

Will show to the screen, but I can't catch it!  I'm sure this is
trivial... any help would be greatly appreciated.



More information about the Python-list mailing list