11

I am looking to write a text directly to my FTP site from python without storing a temp file on disk, something like:

ftp = FTP('ftp.mysite.com')
ftp.login('un','pw')
ftp.cwd('/')
ftp.storbinary('STOR myfile.html', 'text to store', 'rb')

is this even possible? Thank you very much.

Kevin192291
  • 1,925
  • 4
  • 26
  • 43

2 Answers2

16

As the docs say:

Store a file in binary transfer mode. cmd should be an appropriate STOR command: "STOR filename". file is a file object (opened in binary mode) which is read until EOF using its read() method in blocks of size blocksize to provide the data to be stored…

So, you need to give it a file-like object with an appropriate read method.

A string is not a file-like object, but an io.BytesIO is. So:

import io
bio = io.BytesIO(b'text to store')
ftp.storbinary('STOR myfile.html', bio)

Also, notice that I didn't pass that 'rb' argument. The third parameter to storbinary is blocksize, and 'rb' is obviously not a valid block size.


If you need to work with Python 2.5 or earlier, see Dan Lenski's answer.

And if you need to work with Python 2.6-2.7, and performance of the file-like object is important (it isn't here, but there are some cases where it might be), and you only care about CPython, use his answer but with cStringIO in place of StringIO. (Plain StringIO is slow in 2.x, and io.BytesIO is even slower before around 3.3.)

abarnert
  • 354,177
  • 51
  • 601
  • 671
  • 2
    Worked for me but I had to encode the string as BytesIO requiresa bytes-like object. So: `bio = io.BytesIO('text to store'.encode('utf-8'))` – iomihai Feb 24 '18 at 06:57
  • 1
    @iomihai Well, in this case, you could just use a bytes literal, instead of using a str literal and encoding it. But yeah, I'll edit the answer. – abarnert Mar 06 '18 at 23:01
8

Have you tried using a StringIO object, which quacks like a file but is just a string?

from ftplib import *
import StringIO

ftp = FTP('ftp.mysite.com')
ftp.login('un','pw')
ftp.cwd('/')
ftp.storbinary('STOR myfile.html', StringIO.StringIO('text to store'))

EDIT: @abarnert's answer is the Python3 equivalent. Mine is the Python2 version.

Community
  • 1
  • 1
Dan Lenski
  • 76,929
  • 13
  • 76
  • 124
  • 1
    `io` works just as well in 2.6 and 2.7 as it does in 3.x. So there's no reason to use the old version unless you need 2.5 compatibility. – abarnert Sep 09 '14 at 00:55
  • Well, I held onto 1.5 compatibility until 2.4 added some feature I just couldn't live without, so I can understand that… – abarnert Sep 09 '14 at 00:58
  • [`functools.lru_cache`](https://docs.python.org/dev/library/functools.html#functools.lru_cache) has been tempting me to bite the Unicode bullet and go to 3.x for a while now. – Dan Lenski Sep 09 '14 at 01:00
  • 1
    Not that I ever want to take away anyone's motivation to upgrade, but… `lru_cache` is pure-Python, it almost works out-of-the-box with 2.6+, and there's a backport to 2.4+. But `yield from`, `pip` bootstrap, built-in `venv`, `tracemalloc`, `asyncio`, `selectors`, bug fixes and optimizations that will never be backported, saner and more detailed exception hierarchy, chained exceptions, `importlib`, C functions with actual argspecs, improved `inspect`, `concurrent.futures`, … Plus, once you bite the bullet, proper Unicode is a plus, not a stumbling block. – abarnert Sep 09 '14 at 01:10
  • Absolutely, proper Unicode is a clear win both for the programmer and the users... just one that I haven't fully wrapped my head around yet. `yield from` and `importlib` are among the others that have tempted me... `lru_cache` was just a recent example of my gnawing Py3k envy. – Dan Lenski Sep 09 '14 at 01:14