Is there a better way to handle writing csv in text mode and reading in binary mode?
<p>I have code that looks something like this:</p>

<pre><code>import csv
import os
import tempfile

from azure.storage import CloudStorageAccount


account = CloudStorageAccount(
    account_name=os.environ['AZURE_ACCOUNT'],
    account_key=os.environ['AZURE_ACCOUNT_KEY'],
)
service = account.create_block_blob_service()

with tempfile.NamedTemporaryFile(mode='w') as f:
    writer = csv.DictWriter(f, fieldnames=['foo', 'bar'])
    writer.writerow({'foo': 'just an example', 'bar': 'of what I do'})

    with open(f.name, 'rb') as stream:
        service.create_blob_from_stream(
            container_name='test',
            blob_name='nothing_secret.txt',
            stream=stream,
        )
</code></pre>

<p>Now, this is ugly. I don't like having to open the file twice. I know that the Azure API provides a way to upload text and binary, but my file has the potential to be several hundred MB large so I'm not too interested in sticking the whole thing in memory at a time (not that it would be the end of the world, but still).</p>

<p>Azure doesn't support uploading a file in text mode (that I can see), and csv doesn't seem to support writing to a binary file (at least not text data).</p>

<p>Is there a way that I can have two handles to the same file, one in binary and one in text mode? Of course I <em>could</em> write my own file wrapper, but I'd prefer to use something <em>I</em> don't have to maintain. Is there a better way to do this than what I've got?</p>

<p>Files opened in text mode have a <a href="https://docs.python.org/3/library/io.html#io.TextIOBase.buffer" rel="nofollow"><code>buffer</code></a> attribute.  This object is the same one you would get by opening the file in binary mode, the text mode is just a wrapper on top of it.</p>

<p>Open your file in text mode, use it for read it, then seek the buffer back to the start and use it for uploading.  Make sure you use <code>+</code> mode for reading and writing from the same handle.</p>

<pre><code>with tempfile.NamedTemporaryFile(mode='w+') as f:
    ...

    f.seek(0)

    service.create_blob_from_stream(
        ...
        stream=f.buffer,
    )
</code></pre>

<p>You can go the other way too, by opening in binary mode then wrapping with <code>io.TextIOWRapper(f)</code>.</p>

python python-3.x csv azure windows-azure-storage