Invoking internal or external commands of the operating system as well as starting any other application available in the system can be achieved with the help of certain functions defined in two built-in modules - os module and subprocess module.
First, we shall use functions in the os
module.
os.system() Function
This function receives a string argument, which has to be the valid executable command. This function opens a subshell by calling the system()
function, a standard C function. Output if any of the commands will not appear on the Python console.
The function returns 0 on the successful execution of the given command, otherwise, it returns 1.
import os
os.system("dir *.md")
0
The above code snippet will display .md
files in the current folder. It is also possible to call any application whose executable (.exe) is found in the system path.
The following will start calculator application in a Windows OS in a separate window.
import os
os.system("calc")
0
os.popen() function
This function opens a pipe stream for communication with a file like object created by the first argument command. This function has the following signature:
os.popen(cmd, mode, buffering)
- cmd: A string representing command to be called
- mode: Either 'r' or 'w' decides direction of pipe.
- buffering: system's buffering policy
The following command opens an input pipe to read line by line output of given dir command. Output is displayed on console of Python shell.
import os
pipe=os.popen("dir *.md")
print (pipe.read())
Volume in drive C is Windows 10
Volume Serial Number is 540D-CE99
Directory of C:\Users\User\Documents\MyDocs
10/04/2020 12:48 PM 2,182 python-prog1.py
10/07/2020 09:34 PM 4,639 list.md
10/08/2020 12:54 AM 3,696 calc.py
10/06/2020 12:57 AM 3,315 help.md
4 File(s) 13,832 bytes
0 Dir(s) 175,492,399,104 bytes free
The subprocess module of Python's standard library has two functions meant for calling external commands. The purpose of functions in this module is to spawn a new process and connect to IO pipes.
As per PEP 324, it is recommended to use the run()
function to invoke a new process.
subprocess.run() function
This function runs the command in the string argument, waits for the process to complete, and object of CompletedProcess class. Following use case of run() function displays .ipynb files in the current directory on the Python shell and returns CompletedProcess instance with returncode=0 indicating successful execution.
import subprocess
subprocess.run("dir *.ipynb", shell=True)
CompletedProcess(args='dir *.ipynb', returncode=0)
subprocess.call() function
The subprocess.call()
function is a part of older version of this module (before Python 3.5). This is similar to the run()
function, but returns returncode attribute.
import subprocess
subprocess.call("cls", shell=True)
0
Above command calls the DOS command cls
. The current shell terminal is cleared and the function returns 0 as returncode.
- Compare strings in Python
- Convert file data to list
- Convert User Input to a Number
- Convert String to Datetime in Python
- How to count the occurrences of a list item?
- How to flatten list in Python?
- How to merge dictionaries in Python?
- How to pass value by reference in Python?
- Remove duplicate items from list in Python
- Remove items from a list in Python
- repr() vs str() in Python
- How to sort dictionary by value in Python?
- globals() and locals() Method in Python
- Class Attributes vs Instance Attributes in Python