How to upload binary file with ftplib in Python?
Asked Answered
F

2

12

My python2 script uploads files nicely using this method but python3 is presenting problems and I'm stuck as to where to go next (googling hasn't helped).

from ftplib import FTP
ftp = FTP(ftp_host, ftp_user, ftp_pass)
ftp.storbinary('STOR myfile.txt', open('myfile.txt'))

The error I get is

Traceback (most recent call last):
  File "/Library/WebServer/CGI-Executables/rob3/functions/cli_f.py", line 12, in upload
    ftp.storlines('STOR myfile.txt', open('myfile.txt'))
  File "/Library/Frameworks/Python.framework/Versions/3.1/lib/python3.1/ftplib.py", line 454, in storbinary
    conn.sendall(buf)
TypeError: must be bytes or buffer, not str

I tried altering the code to

from ftplib import FTP
ftp = FTP(ftp_host, ftp_user, ftp_pass)
ftp.storbinary('STOR myfile.txt'.encode('utf-8'), open('myfile.txt'))

But instead I got this

Traceback (most recent call last):
  File "/Library/WebServer/CGI-Executables/rob3/functions/cli_f.py", line 12, in upload
    ftp.storbinary('STOR myfile.txt'.encode('utf-8'), open('myfile.txt'))
  File "/Library/Frameworks/Python.framework/Versions/3.1/lib/python3.1/ftplib.py", line 450, in storbinary
    conn = self.transfercmd(cmd)
  File "/Library/Frameworks/Python.framework/Versions/3.1/lib/python3.1/ftplib.py", line 358, in transfercmd
    return self.ntransfercmd(cmd, rest)[0]
  File "/Library/Frameworks/Python.framework/Versions/3.1/lib/python3.1/ftplib.py", line 329, in ntransfercmd
    resp = self.sendcmd(cmd)
  File "/Library/Frameworks/Python.framework/Versions/3.1/lib/python3.1/ftplib.py", line 244, in sendcmd
    self.putcmd(cmd)
  File "/Library/Frameworks/Python.framework/Versions/3.1/lib/python3.1/ftplib.py", line 179, in putcmd
    self.putline(line)
  File "/Library/Frameworks/Python.framework/Versions/3.1/lib/python3.1/ftplib.py", line 172, in putline
    line = line + CRLF
TypeError: can't concat bytes to str

Can anybody point me in the right direction

Fishhook answered 26/5, 2010 at 9:44 Comment(2)
there is nothing py3k-exclusive about this question.Deplore
It's not py3k exclusive but going by how the same code suddenly threw an error (and based on your answer it was right to do so) related to string encoding I assumed it might be.Fishhook
D
35

The issue is not with the command argument, but with the the file object. Since you're storing binary you need to open file with 'rb' flag:

>>> ftp.storbinary('STOR myfile.txt', open('myfile.txt', 'rb'))
'226 File receive OK.'
Deplore answered 26/5, 2010 at 10:10 Comment(1)
At work at the moment, will test it out when I get home and hopefully all will be grand, thanks!Fishhook
S
2

APPEND to file in FTP.

Note: it's not SFTP - FTP only

import ftplib
ftp = ftplib.FTP('localhost')
ftp.login ('user','password')
fin = open ('foo.txt', 'r')
ftp.storbinary ('APPE foo2.txt', fin, 1)

Ref: Thanks to Noah

Strove answered 24/10, 2017 at 4:29 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.