October 4th, 2004, 03:29 PM
Launching applications using Python
Is there anyway to launch the windows application from within a python script. I mean when I want to launch an application on my computer desktop, I double click on it. But is there anyway to do that by a python script?
October 4th, 2004, 03:59 PM
there are several ways to do it.
The simplest is to use os.system(command) to run an external command. This will launch an application - either the executable needs to be on the path, or you need to specify the full path.
The system function will normally wait until the application has been closed before returning, which may or may not be what you want. If you want to launch an application and have the python script continue, then you can use the Windows start command:
#run the windows calculator:
If you want to run a command line program and return the output to python you will need to use one of the popen functions. That is a much bigger topic, so I will not cover it here.
#launch the windows calculator and continue
Dave - The Developers' Coach
October 5th, 2004, 11:14 AM
Hi, im just trying to fathom out some things for a cdrecord gui. Can this command be used the same way with a linux shell? And would it take extra parameters including strings. ie "nano -s $myfile"
If there is some documentation, im sure i can find everything else i need. Sorry if my lingo isnt quite right, (is it a string in python?) but im sure you know what i mean.
October 5th, 2004, 05:42 PM
os.system() will work regardless of platform, just like in C/C++ if you're familiar with any of these. Of course the command you need to issue will probably be dependent on the platform the program is running on. Oh, and yes: it is called a string in Python.
check out the os module: http://www.python.org/doc/2.3.4/lib/module-os.html; you might also want to check out the sys module i.e. you can check the platform Python is running on using sys.platform.
Last edited by netytan; October 5th, 2004 at 05:47 PM.
October 6th, 2004, 02:57 AM
Passing a parameter to notepad using a string:
You do the same with any command that takes command line parameters (on any platform). Note that you may get a console window (xterm) - if this is not what you want then checkout the process spawning commands like spawnv and popen (see python help files for details) A search of this forum will probably show some examples.
>>> import os
>>> fname = "readme.txt"
>>> os.system("notepad"+" " + fname)
October 6th, 2004, 06:50 PM
Another option besides os.system is os.startfile.
os.startfile is identical to clicking on a file (or application) in Windows Explorer or the desktop. By supplying the os.startfile function with the file name, it will open that file in the appropriate application. I'm not sure if it will work under Linux or not.
For further information, see the doc string for the startfile function.
October 6th, 2004, 06:55 PM
os.system! I knew there was a tidier way than os.spawnv() the last time I was trying to remember how to launch programs, but I couldn't remember it. Oh well.