[Tutor] How to create memory backed file?

Modulok modulok at gmail.com
Mon Dec 28 04:57:14 CET 2009


Kent,

Thanks! I think that'll do it. I don't know what this list would do without you!

-Modulok-

On 12/27/09, Kent Johnson <kent37 at tds.net> wrote:
> 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