retrbinary ! how does it work ?

Gabriel Genellina gagsl-py at yahoo.com.ar
Thu Feb 1 04:34:32 EST 2007


En Thu, 01 Feb 2007 06:17:34 -0300, blancmunier1 at yahoo.FR  
<blancmunier1 at yahoo.FR> escribió:

> 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)

(hmm, using file as a variable name is not a good idea, it hides the  
builtin type of the same name)

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

Let's say, if the file name were "abc.txt", you should open the output  
using this name: '../archives/abc.txt'
To build a path from its components, use os.path.join
So, replace open(file,... above, with  
open(os.path.join('../archives',file),...

-- 
Gabriel Genellina




More information about the Python-list mailing list