Created
April 5, 2016 05:49
-
-
Save nitin42/b9d1f2ae2e71b30b70fc2465ce1b2355 to your computer and use it in GitHub Desktop.
Launching Other Programs from Python Interpreter
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#### Launching other programs using python | |
You can launch other program using `Popen()` function present in built in `subprocess()` module. | |
To start an external progra, from your python script,pass filename to subprocess.Popen() | |
For example | |
``` | |
>>> import subprocess | |
>>> subprocess.Popen('C:\\Windows\\System32\\calc.exe') | |
``` | |
Popen has two useful methods | |
``` | |
poll() - The poll() method will return None if the process is still running at the time poll() is called. If the program has | |
terminated, it will return the process’s integer exit code. | |
wait() - The wait() method is like waiting for your friend to finish working on his/her code before you keep working on yours. | |
``` | |
You can also pass command line arguments to Popen() | |
For example | |
``` | |
subprocess.Popen(['C:\\Windows\\notepad.exe', 'C:\\hello.txt']) | |
``` | |
This will not only launch the Notepad application but also have it | |
immediately open the C:\hello.txt file. | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment