As the docs say:
Save the file in binary transfer mode. cmd should be a suitable STOR : "STOR filename" command. file is a file object (opened in binary mode) that is read until the EOF uses the read() method in block size blocks to ensure data is saved ...
So, you need to provide it with a file-like object with the appropriate read method.
The string is not a file-like object, but io.BytesIO . So:
import io bio = io.BytesIO(b'text to store') ftp.storbinary('STOR myfile.html', bio)
Also note that I did not pass this argument to 'rb' . The third storbinary parameter storbinary blocked, and 'rb' is obviously not a valid block size.
If you need to work with Python 2.5 or earlier, see Dan Lensky's answer.
And if you need to work with Python 2.6-2.7, and the performance of the file object is important (it is not here, but there are times when it may be), and you only care about CPython, use its answer, but cStringIO instead of StringIO . (Regular StringIO slow at 2.x, and io.BytesIO even slower at 3.3.)
source share