I want to upload a text string as a file via FTP.
import ftplib
from io import StringIO
file = StringIO()
file.write("aaa")
file.seek(0)
with ftplib.FTP() as ftp:
ftp.connect("192.168.1.104", 2121)
ftp.login("ftp", "ftp123")
ftp.storbinary("STOR 123.txt", file)
This code returns an error:
TypeError: 'str' does not support the buffer interface
This can be a point of confusion in python 3, especially since tools like
csv
will only writestr
, whileftplib
will only acceptbytes
.One way around this problem is to extend
io.BytesIO
to encode yourstr
tobytes
before writing:You can do this as well
Works for me in python 3.