[Tutor] opening a file directly from memory

Dave Angel davea at ieee.org
Sun Nov 15 11:32:55 CET 2009



mjekl at iol.pt wrote:
> <div class="moz-text-flowed" style="font-family: 
> -moz-fixed">davea at ieee.org wrote:
>> (You forgot to send this message to the list, so I'm forwarding it)
>>
>> mjekl at iol.pt wrote:
>>>> davea at ieee.org wrote:
>>>> mjekl at iol.pt wrote:
>>>>> <div class=oz-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
>>>>
>>>
>>> I know what the binary data is from the database. Typically it would 
>>> be some file the OS knows how to open.
>>> Chiefly I'm trying to avoid making the user save the file on some 
>>> location and then go and double click it to open it. This makes no 
>>> sense, since the user has already made the decision to open the file.
>>> Miguel
>>>
>>> Txs Dave.
>>>
>> You forgot to answer the question.  You say "The OS knows how to 
>> open".  Does *your* *program* know what program is needed, to open 
>> this particular binary data?
>>
> Yes. My program knows. A database column stores the complete file name 
> (including extension), and I can be certain the applications will be 
> available to run the file.
> Miguel
>
>
Then just use the subprocess module to invoke the program, passing it 
the filename as an argument.  No need for any double-clicking.

DaveA


More information about the Tutor mailing list