15

I am trying to upload file from windows server to a unix server (basically trying to do FTP). I have used the code below

#!/usr/bin/python
import ftplib
import os
filename = "MyFile.py"
ftp = ftplib.FTP("xx.xx.xx.xx")
ftp.login("UID", "PSW")
ftp.cwd("/Unix/Folder/where/I/want/to/put/file")
os.chdir(r"\\windows\folder\which\has\file")
ftp.storbinary('RETR %s' % filename, open(filename, 'w').write)

I am getting the following error:

Traceback (most recent call last):
  File "Windows\folder\which\has\file\MyFile.py", line 11, in <module>
    ftp.storbinary('RETR %s' % filename, open(filename, 'w').write)
  File "windows\folder\Python\lib\ftplib.py", line 466, in storbinary
    buf = fp.read(blocksize)
AttributeError: 'builtin_function_or_method' object has no attribute 'read'

Also all contents of MyFile.py got deleted .

Can anyone advise what is going wrong.I have read that ftp.storbinary is used for uploading files using FTP.

4 Answers 4

17

If you are trying to store a non-binary file (like a text file) try setting it to read mode instead of write mode.

ftp.storlines("STOR " + filename, open(filename, 'rb'))

for a binary file (anything that cannot be opened in a text editor) open your file in read-binary mode

ftp.storbinary("STOR " + filename, open(filename, 'rb'))

also if you plan on using the ftp lib you should probably go through a tutorial, I'd recommend this article from effbot.

Sign up to request clarification or add additional context in comments.

6 Comments

the contents of MyFile.py don't get deleted . But I am still egtting the error.
Using storlines assumes that myfile is a text file. For other files (images, movies, archives, basically anything that you cannot open in a text editor and read) you should use storbinary and open the file with 'rb' instead of just 'r
according to bugs.python.org/issue6822 non-binary files should also be opened with 'rb' flag, but after that still uploaded with storlines(). just faced that problem with 'r' and 'Type str doesn't support the buffer AP' exception for the link provided.
hello @john, How about if I want to move the wav file instead of binary and text file, May I know how can I move the wav file to FTP server?
@Susan I believe a wav file is a binary file.
|
9

Combined both suggestions. Final answer being

#!/usr/bin/python
import ftplib
import os
filename = "MyFile.py"
ftp = ftplib.FTP("xx.xx.xx.xx")
ftp.login("UID", "PSW")
ftp.cwd("/Unix/Folder/where/I/want/to/put/file")
os.chdir(r"\\windows\folder\which\has\file")
myfile = open(filename, 'r')
ftp.storlines('STOR ' + filename, myfile)
myfile.close()

2 Comments

Using storlines assumes that myfile is a text file. For other files (images, movies, archives, basically anything that you cannot open in a text editor and read) you should use storbinary and open the file with 'rb' instead of just 'r'
At least in the current version, you need to open the file in binary mode ('rb') to send it, otherwise you get an error. EDIT: didn't realize someone had already said this ^
3

try making the file an object, so you can close it at the end of the operaton.

myfile = open(filename, 'w')
ftp.storbinary('RETR %s' % filename, myfile.write)

and at the end of the transfer

 myfile.close()

this might not solve the problem, but it may help.

Comments

0

ftplib supports the use of context managers so you can make it even simpler as such

    with ftplib.FTP('ftp_address', 'user', 'pwd') as ftp, open(file_path, 'rb') as file:
        ftp.storbinary(f'STOR {file_path.name}', file)
        ...

This way you are robust against both file and ftp issues without having to insert try/except/finally blocks. And well, it's pythonic.

PS: since it uses f-strings is python >= 3.6 only but can easily be modified to use the old .format() syntax

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.