Start application & continue after app exits

Paul McNett p at
Thu Jun 9 18:07:47 CEST 2005

Guy Lateur wrote:
> I was wondering if it would be possible to launch an application, block 
> until the app exits, and do some cleanup afterwards.

> Maybe an example will be clearer: I would like to make a temperary (text) 
> file, open it with MS Word for the user to edit/layout/print, and then 
> delete the temp file after the user shuts down Word. Is this feasible?

Something like:

import os
import tempfile

testString = "Life is but a dream."
fileName = tempfile.mktemp()
print fileName
open(fileName, "w").write(testString)
os.system("gedit %s" % fileName)

Note that I'm on Linux, and instead of launching Word I'm launching an 
editor named "gedit". You could likely change that call to winword.exe, 
but I haven't tested it on Windows.

Also note that this method of creating tempfiles is technically unsafe, 
as it is theoretically possible that another process would create a file 
of the same name in the same directory and then try to use it, resulting 
in a race condition between the two processes. This is practically 
unlikely, however, and I'm a pragmatist.

Paul McNett

More information about the Python-list mailing list