ftplib callbacks
Steve Holden
steve at holdenweb.com
Sun Nov 21 16:11:25 EST 2004
Matija Papec wrote:
> I would like to reimplement ftplib "nlst" using ftplib.dir (ftp server
> doesn't support nlst) so I'm trying to guess how to use ftp callbacks.
> Any help is appreciated.
> tia!
>
> ============
> #!/usr/bin/python
>
> # nlst vs list
>
> import pprint
> pp = pprint.PrettyPrinter(indent=4)
>
> import ftplib
>
> def myfilter(line):
> return line
>
> ftp = ftplib.FTP("localhost")
> ftp.login("mpapec", "island88")
> #print ftp.retrlines("LIST")
> dir = ftp.dir("", myfilter)
> print dir
>
>
The callback is called each time the FTP client softwrae has something
to deal with. Try (untested):
lines = []
def myfilter(line):
global lines
lines.append(line)
with the remainder of your code unchanged. The lines list will then
contain a list of all lines.
The problem with your current callback is that its return value is
ignored by the FTP module that calls it.
regards
Steve
--
http://www.holdenweb.com
http://pydish.holdenweb.com
Holden Web LLC +1 800 494 3119
More information about the Python-list
mailing list