[Tutor] How to create memory backed file?

Kent Johnson kent37 at tds.net
Mon Dec 28 03:07:50 CET 2009


On Sun, Dec 27, 2009 at 3:36 AM, Modulok <modulok at gmail.com> wrote:
> List,
>
> How do I create a file which exists only in memory? (Think diskless.)
>
> I need to create an in-memory file. I need to pass this file, as a
> regular file object, to a subprocess. Unfortunately, the 'mmap'
> module's file-like object doesn't appear to work quite like a regular
> file would. (I simply assume that mmap would be the way to go. Other
> suggestions welcome!) That, or I'm doing something terribly wrong.

How about using a pipe for stdin? This seems to work:

import sys
from subprocess import Popen, PIPE

fishes = "one fish\ntwo fish\nred fish\nblue fish\n"

proc1 = Popen(['cat'], stdin=PIPE, stdout=sys.stdout, stderr=sys.stderr)
proc1.stdin.write(fishes)
proc1.stdin.close()

Kent


More information about the Tutor mailing list