retrbinary ! how does it work ?

Diez B. Roggisch deets at nospam.web.de
Thu Feb 1 04:33:59 EST 2007


blancmunier1 at yahoo.FR wrote:

> Hello dear community !
> 
> I'm a bit ashamed to ask such an easy question, but I didn't find my
> answer on previous posts.
> I'd like to copy files with FTP protocol in a subdirectory.
> So far, my code look like that :
> 
>   import ftplib
>   session = ftplib.FTP('222.33.44.55','usr','pwd')
>   session.cwd('/')
>   files = session.nlst()
>   for file in files:
>     session.retrbinary('RETR '+file, open(file, 'wb').write)
> 
> It does work but the files are copied in the same directory as the
> python file. And I would like to copy the file in this relative
> directory :  ../archives
> For example, if the python file is in this directory : C:\SCOR\Bat\,
> the FTP files gotta be in C:\SCOR\archives\ .
> 
> Can anyone help me.

The problem is your callback-function that you create like this:

open(file, 'wb').write

If all you pass here is file, where else than in the current working
directory do you expect files to appear?

Either provide a full destination path like this

open(os.path.join('../', file), 'wb').write

or change the current working directory beforehand, using os.cwd.

Diez



More information about the Python-list mailing list