[python-win32] os.startfile mysteriously failing

King Simon-NFHD78 simon.king at motorola.com
Tue Apr 22 12:47:37 CEST 2008


Thomas Heller wrote:
> 
> Some remarks that may or may not be useful:
> 
> AFAIK, a single threaded COM apartment needs to run a message loop,
> according to the COM rules.  If the OP has a gui-application, and
> calls os.startfile from the main thread, then this should be 
> no problem.
> 
> If he has a console application, there normally is no message loop.
> 
> Looking into the MSDN docs for ShellExecuteEx [1], *this* 
> function allows
> to set additional flags in the SHELLEXECUTEINFO structure 
> that you pass to it.
> It seems that SEE_MASK_FLAG_DDEWAIT may be something worth to 
> try out [2]:
> 
> """
> The SEE_MASK_FLAG_DDEWAIT flag must be specified if the 
> thread calling ShellExecuteEx
> does not have a message loop or if the thread or process will 
> terminate soon after
> ShellExecuteEx returns. Under such conditions, the calling 
> thread will not be available
> to complete the DDE conversation, so it is important that 
> ShellExecuteEx complete the
> conversation before returning control to the calling 
> application. Failure to complete
> the conversation can result in an unsuccessful launch of the document.
> """
> 
> [1] http://msdn2.microsoft.com/en-us/library/bb762154.aspx
> [2] http://msdn2.microsoft.com/en-us/library/bb759784(VS.85).aspx
> 
> Thomas
> 

Hi Thomas,

Thanks for responding. I do have a GUI application (using wxPython), and
I am calling os.startfile from the main thread.

However, thanks to yours and Tim's suggestions, I've done some more
experimenting, and have discovered the following:

I can get webbrowser.open('http://www.python.org') to fail fairly
reliably by ensuring that I have lots of tabs open in Firefox (and the
option for showing my windows and tabs from last time on startup), then
exiting Firefox, then running webbrowser.open('http://www.python.org').
Firefox starts, and it opens a tab for www.python.org, but it also loads
all my tabs from the previous session. This seems to take longer than
startfile/ShellExecute is prepared to wait, and so it fails with
WindowsError: [Error 2] The system cannot find the file specified:
'http://www.python.org'. I'm fairly convinced that this is the same
problem Tim referred to about modal dialog boxes. (Out of interest,
running 'start http://www.python.org' from a command prompt has a
similar problem - it blocks while Firefox is loading all its tabs, but
eventually gives up waiting and shows a dialog box saying that the file
cannot be found). Perhaps this should be considered a bug in Firefox.

If the file I'm trying to launch has a 'simple' file association (such
as KML which has a command of 'C:\Program Files\Google\Google
Earth\googleearth.exe "%1"'), then os.startfile returns almost
immediately. If the file association uses DDE (such as CSV when
associated with Excel), then startfile doesn't return until the
application has finished opening the file. I'm not actually certain
whether the message loop is necessary in this situation - I was calling
startfile from a command prompt and it seemed to be working.

I've not managed to recreate the 'file not found' error with a CSV file,
nor the 'parameter is incorrect' error with the KML file.

I think I may make my own startfile function using ShellExecuteEx. All I
really want is a 'fire-and-forget' function (for example, if there is no
registered handler for the file extension, I would rather the user saw
the shell's 'What application would you like to use' dialog than having
to make my own). I'll experiment with the SEE_MASK_ASYNCOK flag.

Thank you both for your help,

Simon


More information about the python-win32 mailing list