<p>In Python you can use <a href="https://docs.python.org/library/struct.html" rel="noreferrer">StringIO</a> for a file-like buffer for character data. <a href="https://docs.python.org/library/mmap.html" rel="noreferrer">Memory-mapped file</a> basically does similar thing for binary data, but it requires a file that is used as the basis. Does Python have a file object that is intended for binary data and is memory only, equivalent to Java's <a href="http://java.sun.com/javase/6/docs/api/java/io/ByteArrayOutputStream.html" rel="noreferrer">ByteArrayOutputStream</a>?</p>

<p>The use-case I have is I want to create a ZIP file in memory, and <a href="https://docs.python.org/library/zipfile.html" rel="noreferrer">ZipFile</a> requires a file-like object.</p>

[{'answer_id': 25123, 'author': 'mmattax', 'author_id': 1638, 'author_profile': 'https://Stackoverflow.com/users/1638', 'pm_score': 2, 'selected': False, 'text': '<p>Look at the struct package: <a href="https://docs.python.org/library/struct.html" rel="nofollow noreferrer">https://docs.python.org/library/struct.html</a>, it allows you to interpret strings as packed binary data.</p>\n\n<p>Not sure if this will completely answer your question but you can use struct.unpack() to convert binary data to python objects.</p>\n\n<pre>\n<code>\nimport struct\nf = open(filename, "rb")\ns = f.read(8)\nx, y = struct.unpack(">hl", s)\n</code>\n</pre>\n\n<p>int this example, the ">" tells to read big-endian the "h" reads a 2-byte short, and the "l" is for a 4-byte long. you can obviously change these to whatever you need to read out of the binary data...</p>\n'}, {'answer_id': 25180, 'author': 'Henrik Gustafsson', 'author_id': 2010, 'author_profile': 'https://Stackoverflow.com/users/2010', 'pm_score': 5, 'selected': False, 'text': '<p>As long as you don\'t try to put any unicode data into your <code>StringIO</code> and you are careful NOT to use <code>cStringIO</code> you should be fine.</p>\n\n<p>According to the <a href="https://docs.python.org/library/stringio.html" rel="nofollow noreferrer">StringIO</a> documentation, as long as you keep to either unicode or 8-bits everything works as expected. Presumably, <code>StringIO</code> does something special when someone does a <code>f.write(u"asdf")</code> (which ZipFile does not do, to my knowledge). Anyway;</p>\n\n<pre><code>import zipfile\nimport StringIO\n\ns = StringIO.StringIO()\nz = zipfile.ZipFile(s, "w")\nz.write("test.txt")\nz.close()\nf = file("x.zip", "w")\nf.write(s.getvalue())\ns.close()\nf.close()\n</code></pre>\n\n<p>works just as expected, and there\'s no difference between the file in the resulting archive and the original file.</p>\n\n<p>If you know of a particular case where this approach does not work, I\'d be most interested to hear about it :)</p>\n'}, {'answer_id': 7357938, 'author': 'akhan', 'author_id': 828885, 'author_profile': 'https://Stackoverflow.com/users/828885', 'pm_score': 8, 'selected': True, 'text': '<p>You are probably looking for <a href="http://docs.python.org/release/3.1.3/library/io.html#binary-i-o">io.BytesIO</a> class. It works exactly like StringIO except that it supports binary data:</p>\n\n<pre><code>from io import BytesIO\nbio = BytesIO(b"some initial binary data: \\x00\\x01")\n</code></pre>\n\n<p>StringIO will throw TypeError:</p>\n\n<pre><code>from io import StringIO\nsio = StringIO(b"some initial binary data: \\x00\\x01")\n</code></pre>\n'}]
['https://Stackoverflow.com/questions/25116', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/2679/']