Skip to content

Instantly share code, notes, and snippets.

@nitin42
Created April 5, 2016 05:49
Show Gist options
  • Save nitin42/b9d1f2ae2e71b30b70fc2465ce1b2355 to your computer and use it in GitHub Desktop.
Save nitin42/b9d1f2ae2e71b30b70fc2465ce1b2355 to your computer and use it in GitHub Desktop.
Launching Other Programs from Python Interpreter
#### 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