Using a Callback Function - ftplib

S

seldan24

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.
 
S

seldan24

Il Mon, 17 Aug 2009 10:43:33 -0700 (PDT), seldan24 ha scritto:
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'])

Why bother with retrlines? Use the provided higer level fuctions:

remotefiles = []
ftp.dir(remotefiles.append)

or, if you prefer nlst

remotefiles = ftp.nlst()

regards
david

I didn't even notice the higher level methods. I changed the
retrieval line to:

ftp.nlst("testfile*.txt")

This works great. The result is even captured in an array. I really
have no idea what the difference between a LIST and NLST is within
FTP. Never delved that deep into it. I did notice that an NLST will
return a specific FTP code if a file doesn't exist, whereas a LIST
doesn't. So, I ended up using NLST as that'll generate an
ftplib.error_perm exception. Based on if the job cares if a file is
not available or not (some do, some don't), I'll either exit, or
continue on with the file loop.

Anyway, thanks again, works perfectly, next time I'll try to scroll
down and read a bit more prior to posting!
 
N

nitebirdz

I didn't even notice the higher level methods. I changed the
retrieval line to:

ftp.nlst("testfile*.txt")

This works great. The result is even captured in an array. I really
have no idea what the difference between a LIST and NLST is within
FTP. Never delved that deep into it. I did notice that an NLST will
return a specific FTP code if a file doesn't exist, whereas a LIST
doesn't. So, I ended up using NLST as that'll generate an
ftplib.error_perm exception. Based on if the job cares if a file is
not available or not (some do, some don't), I'll either exit, or
continue on with the file loop.

The following thread from a NetBSD mailing list may help clarify this
issue:

http://mail-index.netbsd.org/netbsd-users/2001/01/30/0016.html


NLST returns a machine-readable list of names, while LIST returns a
human-readable list. Hene the presence of the FTP code in the case of
NLST.
 
N

Nitebirdz

I didn't even notice the higher level methods. I changed the
retrieval line to:

ftp.nlst("testfile*.txt")

This works great. The result is even captured in an array. I really
have no idea what the difference between a LIST and NLST is within
FTP. Never delved that deep into it. I did notice that an NLST will
return a specific FTP code if a file doesn't exist, whereas a LIST
doesn't. So, I ended up using NLST as that'll generate an
ftplib.error_perm exception. Based on if the job cares if a file is
not available or not (some do, some don't), I'll either exit, or
continue on with the file loop.

The following thread from a NetBSD mailing list may help clarify this
issue:

http://mail-index.netbsd.org/netbsd-users/2001/01/30/0016.html


NLST returns a machine-readable list of names, while LIST returns a
human-readable list. Hene the presence of the FTP code in the case of
NLST.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

ftplib limitations? 9
ftplib callbacks 2
ftplib - Did the whole file get sent? 6
ftplib returns EOFError 0
i have error then use ftplib 1
CallBack 0
using ftplib 5
utf8 and ftplib 5

Members online

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top