0

I am trying to implement FTP download from a UNIX server into a windows box. I have got this far(code below) , but get an error specifying a file object is required but str is passed

Code

#!/usr/bin/python
import ftplib
filename = "filename"
ftp = ftplib.FTP("xx.xxx.xxx.xxx")
ftp.login("uid", "psw")
ftp.cwd("/my/location")
print filename
ftp.retrbinary('RETR %s' % filename, file.write)

Error

Traceback (most recent call last):
  File "FTP.py", line 10, in <module>
    ftp.retrbinary('RETR %s' % filename, file.write)
  File "/usr/lib/python2.6/ftplib.py", line 399, in retrbinary
    callback(data)
TypeError: descriptor 'write' requires a 'file' object but received a 'str'

Can anyone advise how to sort this. Also if possible where can I get some sample examples to learn Python FTP.

0

3 Answers 3

1

You need to open a local file to write to.

Change

ftp.retrbinary('RETR %s' % filename, file.write)

to

ftp.retrbinary('RETR %s' % filename, open(filename, 'wb').write)
Sign up to request clarification or add additional context in comments.

3 Comments

It did run succesfully. I am not sure where the file goes though ? Where did it get downloaded? Or did it at all ?
@misguided: That's controlled by the filename argument to open. If there's no path, the file will be created in the script's current working directory.
so how do I write the file into my local windows system ? The scripts is being run on a unix server .
0
ftp.retrbinary('RETR %s' % filename, open('myoutputfile.txt', 'wb').write)

Comments

0

Open the file you are going to write to using with open. Supposing you are reading from file server_filename and writing to the file local_filename:

with open(local_filename, 'wb') as opened_file:
    ftp.retrbinary('RETR %s' % server_filename, opened_file.write)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.