starting programs from python script on windows

Tim Golden mail at timgolden.me.uk
Mon Jan 28 05:36:42 EST 2008


Benedict Verheyen wrote:
> i want to automate starting programs on my windows machine and i want
> to do it with windows.
> This is a sample script:
> 
> from subprocess import Popen, PIPE
> import time
> 
> print " Starting app 1"
> time.sleep(1)
> try:
>      p1 = Popen(["C:\Program Files\Microsoft 
> Office\OFFICE11\OUTLOOK.EXE"], stdout=PIPE)
> except Exception, e:
>      print "Error on startup app 1 %s " % str(e)
> 
> print " Starting app 2"
> time.sleep(1)
> 
> try:
>      p2 = Popen(["C:\Windows\system32\cmd.exe"], stdout=PIPE)
> except Exception, e:
>      print "Error on startup app 2 %s " % str(e)
> 
> It start it from a batch file:
> SET PYTHONPATH=C:\Python25
> 
> rem - path to script to execute
> %PYTHONPATH%\python.exe C:\login.py
> 
> This is the result:
> 
> C:\>C:\Python25\python.exe C:\login.py
>   Starting app 1
>   Starting app 2
> Het proces heeft geprobeerd naar een niet-bestaande sluis te schrijven.
> Het proces heeft geprobeerd naar een niet-bestaande sluis te schrijven.
> Het proces heeft geprobeerd naar een niet-bestaande sluis te schrijven.
> 
> 1. I get an error message saying the process has tried to write to a non 
> existing pipe.
> 
> 2. Order of execution isn't respected: it prints the 2 messages and then 
> it tries to start the programs. Outlook is started but the command 
> prompt not.
> 
> Anyway, if it works, i would like to start using python to drive the 
> startup scripts of the users on the system.
> 
> How can i use python to start several programs as i would otherwise do 
> manually and keep the order i want?

[Takes a big, deep breath]

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



More information about the Python-list mailing list