Python write create file directly in FTP
Asked Answered
K

2

12

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.

Kenton answered 9/9, 2014 at 0:43 Comment(0)
L
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.)

Lila answered 9/9, 2014 at 0:53 Comment(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'))Whitleather
@Whitleather 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.Lila
P
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.

Panties answered 9/9, 2014 at 0:54 Comment(5)
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.Lila
Well, I held onto 1.5 compatibility until 2.4 added some feature I just couldn't live without, so I can understand that…Lila
functools.lru_cache has been tempting me to bite the Unicode bullet and go to 3.x for a while now.Panties
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.Lila
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.Panties

© 2022 - 2024 — McMap. All rights reserved.