newbie needs a little help... trying to write an ftp script.

google account mynews44 at
Tue Jul 29 20:35:35 CEST 2003

Wojtek Walczak posted a simple example:

<snipped my original post>> 

> import ftplib,sys
> ftp = ftplib.FTP('')
> ftp.login('anonymous','qwe at')
> print ftp.retrlines('LIST')

I think it works the same if I use print or simply

> ftp.cwd('pub/python')
> print ftp.retrlines('LIST')

I am interested in reading this into a string so that I can process
it.  I need to get the name of each file in teh directory so that I
can do a loop to retr them all; ie,  strip out all directories,  and
then strip out all the extra information (permissions, dates.  I try
stuff like

SomeString = ftp.retrlines('list')

but this only seems to read in the return string:

print SomeString
226 Transfer Complete

IS there anyway I can read in the whole thing?   It is obviously
taking the string as being the only part between the single quotes... 
I don't know how to read the rest into a string.

> print ftp.retrbinary('retr README', sys.stdout.write)

This is the line I am most interested in!  I assume that the callback
has something to do with how the program handles the data it is
retrieving,  and that to save the file it is retrieving I have to open
a file,  write the file,  then close the file...  This line would be
the writing of the file,  I am guessing.  something like

filename = open(r'd:\downloads\' + file, 'w')
ftp.retrbinary('retr README', filename)

And that I can run this thing through a loop for each of the filenames
that I get outta the first string.

Am I completely way offbase here with what I am attempting?  Is this
going to actually download the files from the ftp server to the local
hardware?  Once I can read the output into a string I guess I can get
kicked off.  This is my current hurdle.

Thanks for looking!  


More information about the Python-list mailing list