[Tutor] opening a file directly from memory

Dave Angel davea at ieee.org
Sun Nov 15 03:39:59 CET 2009



mjekl at iol.pt wrote:
> <div class="moz-text-flowed" style="font-family: -moz-fixed">I'm 
> wondering if I must save a file to memory before opening it. By 
> opening I mean displaying it to the user.
>
> I have a BLOB field in a db and I have managed to read the blob into a 
> binary fileobject. I've also managed to write it to disk and then I 
> open it by doubleclicking on it. But I was wondering:
>
> 1. How to open the file directly from code (and not by double clicking):
> I'm aware of os.startfile on Win by I'm on a Mac now, so I rather have 
> a cross-platform way of accomplishing this.
>
> 2. If there is any Python module that takes care of saving and 
> cleaning temp files in an OS transparent way?
>
> Txs,
> Miguel
>

You don't say what this binary data is.  Is there a specific program 
that should be launched to "display it to the user" ?  Or do you have to 
keep this general?

If you know what the necessary program is, you could use subprocess 
module to launch it.  But I don't know enough about the Mac to know how 
to do the Mac equivalent of  os.startfile

As for avoiding the use of a file, that depends entirely on the program 
you're launching.  Some programs can be told to get their data from 
stdin.  If that's the case, there's a way to provide stdin directly from 
Python, using subprocess.

As for temporary files, consider tempfile module. I haven't used it, but 
it looks promising. 

HTH,
DaveA


More information about the Tutor mailing list