starting programs from python script on windows

Benedict Verheyen benedict.verheyen at gmail.com
Mon Jan 28 09:24:19 EST 2008


Tim Golden schreef:
<snip>


> OK. You've got a few misunderstandings in there. Nothing too major,
> but it's worth sorting them out.
> 
> 1) If you just want to kick off a program and that's it, say as part of
> some kind of startup process, then you can just use the subprocess.call
> convenience function. The business with stdout=PIPE is for communicating
> with (usually console-based) programs which read and write to the
> console.
> 
> 2) The optional PYTHONPATH env var is used *internally* to Python as
> one way of determining the path to search for Python modules *after
> you've got Python running*. To run Python itself, you either need
> to ensure the python.exe is already in the standard PATH env var,
> or look for it in its conventional place: c:\python25\python.exe.
> (Or make some other arrangement according to local convention etc.)
> 
> There was a thread here recently about using Python as part of a
> login script -- which is what I think you're doing here. I think,
> because of the uncertain interaction between the Workstation in
> effect when you're logging in as opposed to the Workstation which
> owns the user's desktop, you might do better to have some technique
> for adding to the [Startup] entry on the Start Menu if all you want
> to do is to start programs.
> 
> All that said, here's some sample code which just kicks off a
> batch of programs. Note that I'm use os.startfile because that
> will use ShellExecute which honours app path shortcuts, making
> common things like MS Office apps much easier. You could
> equivalently use subprocess.call but then you either have
> to hardcode application paths or use FindExectable against an
> arbitrary associated doc to find the right place.
> 
> <code - untested>
> import os
> 
> programs = [
>    "outlook.exe",
>    "cmd.exe",
>    "winword.exe",
>    "c:/temp/helpfile.pdf"
> ]
> for program in programs:
>    os.startfile (program)
> 
> </code>
> 
> The last entry -- helpfile.pdf -- is to illustrate that os.startfile
> can "start" documents as well as executable programs.
> 
> TJG

Tim,

that seems to work ok. I will do some more testing but it looks good,

Thanks!
Benedict




More information about the Python-list mailing list