retrbinary ! how does it work ?

B

blancmunier1

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
 
D

Diez B. Roggisch

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
 
G

Gabriel Genellina

En Thu, 01 Feb 2007 06:17:34 -0300, (e-mail address removed)
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),...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top