By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,876 Members | 1,200 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,876 IT Pros & Developers. It's quick & easy.

retrbinary ! how does it work ?

P: n/a
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.

Thanks a lot by advance.

Yvan Blancmunier

Feb 1 '07 #1
Share this Question
Share on Google+
3 Replies


P: n/a
bl**********@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
Feb 1 '07 #2

P: n/a
En Thu, 01 Feb 2007 06:17:34 -0300, bl**********@yahoo.FR
<bl**********@yahoo.FRescribió:
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

Feb 1 '07 #3

P: n/a
Thanks a lot Diez and Gabriel.
It works perfectly !
Have a nice day
Yvan

Feb 1 '07 #4

This discussion thread is closed

Replies have been disabled for this discussion.