|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
||||
|
||||
|
Launching applications using Python
Hello,
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? |
|
#2
|
|||
|
|||
|
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. e.g. Code:
#run the windows calculator:
import os
os.system('calc.exe')
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: Code:
#launch the windows calculator and continue
import os
os.system('start calc.exe')
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. Dave - The Developers' Coach |
|
#3
|
|||
|
|||
|
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. Thanks. |
|
#4
|
||||
|
||||
|
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. Quote:
Mark. Last edited by netytan : October 5th, 2004 at 05:47 PM. |
|
#5
|
||||
|
||||
|
Passing a parameter to notepad using a string:
Code:
>>> import os
>>> fname = "readme.txt"
>>> os.system("notepad"+" " + fname)
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. grim ![]()
__________________
*** Experimental Python Markup CGI V2 *** |
|
#6
|
|||
|
|||
|
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. |
|
#7
|
|||
|
|||
|
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.
|
![]() |
| Viewing: Dev Shed Forums > Programming Languages > Python Programming > Launching applications using Python |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|