In this Python tutorial, we will see how to execute an external program or call a system command in Python.
Table Of Contents
Let’s dive into our tutorial.
What is a System command
A System command in Python is used to run executable binaries to do a particular task.
Examples of System commands
1. dir: The dir command is a System Command used to display all files and subfolders present in directory 2. ipconfig : The ipconfig command is used to display all network information. 3. mkdir : The mkdir command is used to create new directory or folder in the filesystem.
There are the multiple ways to execute a program or call a system command using Python. Let’s discuss them one by one.
Run system command using call() function of subprocess module
The subprocess module has a call() function. It is used to run the system commands in Python. The call() method runs a command, waits for the command to complete, then returns the return code.
Syntax:
Frequently Asked:
- Python : Check if a process is running by name and find it’s Process ID (PID)
- Execute a program or call a system command in Python
- Python : Get List of all running processes and sort by highest memory usage
subprocess.run(command, shell)
- Parameters:
- command = system command.
- shell = boolean value, to specify use shell or not.
- Returns:
- Returns return code.
Approach
- Import subprocess library.
- Pass the system command to call() method and set shell=True.
- The output will be printed
Example:
import subprocess # Passing the command to call() method subprocess.call("dir", shell=True)
Output:
Volume in drive C is OS Volume Serial Number is 5CA3-488D Directory of C:\Users\shiresha\Desktop\geeks_dir 21-05-2022 20:31 <DIR> . 21-05-2022 20:31 <DIR> .. 05-03-2022 14:30 <DIR> question1 17-03-2022 21:53 <DIR> question10 22-03-2022 15:24 <DIR> question11 25-03-2022 13:49 <DIR> question12 22-03-2022 17:59 <DIR> question13 25-03-2022 13:52 <DIR> question14 07-04-2022 21:51 <DIR> question15 20-04-2022 08:59 <DIR> question16 04-05-2022 09:24 <DIR> question17 29-04-2022 17:28 <DIR> question18 04-05-2022 09:24 <DIR> question19 24-02-2022 20:23 <DIR> question2 02-05-2022 07:18 <DIR> question20 08-05-2022 13:23 <DIR> question21 07-05-2022 20:47 <DIR> question22 08-05-2022 14:30 <DIR> question23 12-05-2022 20:31 <DIR> question24 21-05-2022 19:51 <DIR> question25 23-02-2022 22:42 <DIR> question3 05-03-2022 15:36 <DIR> question4 07-03-2022 09:51 <DIR> question5 07-03-2022 14:38 <DIR> QUESTION6 12-03-2022 10:18 <DIR> question7 12-03-2022 11:18 <DIR> question8 13-03-2022 14:24 <DIR> question9 09-05-2022 08:59 <DIR> temp 1 File(s) 355 bytes 28 Dir(s) 41,057,894,400 bytes free
Run system command using run() function of subprocess module
The subprocess module has a run() function. It is used to run the system commands in python. The run() method runs a command, waits for the command to complete, then returns the return code.
Syntax:
subprocess.run()(command, shell)
- Parameters:
- command = system command.
- shell = boolean value, to specify use shell or not.
- Returns:
- Returns return code.
Approach
- Import subprocess library.
- Pass the system command to run() method and set shell=True.
- The output will be printed
Example:
import subprocess # Pass the command to run() method subprocess.run("dir", shell=True)
Output:
Volume in drive C is OS Volume Serial Number is 5CA3-488D Directory of C:\Users\shiresha\Desktop\geeks_dir 21-05-2022 20:31 <DIR> . 21-05-2022 20:31 <DIR> .. 05-03-2022 14:30 <DIR> question1 17-03-2022 21:53 <DIR> question10 22-03-2022 15:24 <DIR> question11 25-03-2022 13:49 <DIR> question12 22-03-2022 17:59 <DIR> question13 25-03-2022 13:52 <DIR> question14 07-04-2022 21:51 <DIR> question15 20-04-2022 08:59 <DIR> question16 04-05-2022 09:24 <DIR> question17 29-04-2022 17:28 <DIR> question18 04-05-2022 09:24 <DIR> question19 24-02-2022 20:23 <DIR> question2 02-05-2022 07:18 <DIR> question20 08-05-2022 13:23 <DIR> question21 07-05-2022 20:47 <DIR> question22 08-05-2022 14:30 <DIR> question23 12-05-2022 20:31 <DIR> question24 21-05-2022 19:51 <DIR> question25 23-02-2022 22:42 <DIR> question3 05-03-2022 15:36 <DIR> question4 07-03-2022 09:51 <DIR> question5 07-03-2022 14:38 <DIR> QUESTION6 12-03-2022 10:18 <DIR> question7 12-03-2022 11:18 <DIR> question8 13-03-2022 14:24 <DIR> question9 09-05-2022 08:59 <DIR> temp 1 File(s) 355 bytes 28 Dir(s) 41,057,894,400 bytes free
Run system command using system() function of os module
The os module has a system() function. It is used to run the system commands in python.
Syntax:
os.system(command)
- Parameters:
- command = system command to be executed
- Returns:
- Returns value returned by the system shell after running command.
Approach
- Import os library.
- Pass the system command to system() method.
- The system method() return the value returned by the system shell after running command.
Example:
import os # Pass the system command to system method os.system("dir")
Output:
Volume in drive C is OS Volume Serial Number is 5CA3-488D Directory of C:\Users\shiresha\Desktop\geeks_dir 21-05-2022 20:31 <DIR> . 21-05-2022 20:31 <DIR> .. 05-03-2022 14:30 <DIR> question1 17-03-2022 21:53 <DIR> question10 22-03-2022 15:24 <DIR> question11 25-03-2022 13:49 <DIR> question12 22-03-2022 17:59 <DIR> question13 25-03-2022 13:52 <DIR> question14 07-04-2022 21:51 <DIR> question15 20-04-2022 08:59 <DIR> question16 04-05-2022 09:24 <DIR> question17 29-04-2022 17:28 <DIR> question18 04-05-2022 09:24 <DIR> question19 24-02-2022 20:23 <DIR> question2 02-05-2022 07:18 <DIR> question20 08-05-2022 13:23 <DIR> question21 07-05-2022 20:47 <DIR> question22 08-05-2022 14:30 <DIR> question23 12-05-2022 20:31 <DIR> question24 21-05-2022 19:51 <DIR> question25 23-02-2022 22:42 <DIR> question3 05-03-2022 15:36 <DIR> question4 07-03-2022 09:51 <DIR> question5 07-03-2022 14:38 <DIR> QUESTION6 12-03-2022 10:18 <DIR> question7 12-03-2022 11:18 <DIR> question8 13-03-2022 14:24 <DIR> question9 09-05-2022 08:59 <DIR> temp 1 File(s) 355 bytes 28 Dir(s) 41,057,894,400 bytes free
Run system command using popen() function of os module
The os module has a popen() function. It is used to run the system commands in python.
Syntax:
os.popen(command)
- Parameters:
- command = system command to be executed
- Returns:
- Returns a file object connected to the pipe.
Approach
- Import os library.
- Pass the system command to popen() method.
- The popen method will return a file object and read it using the read() method.
Example:
import os # Pass the system command to popen method print(os.popen("dir").read())
Output:
Volume in drive C is OS Volume Serial Number is 5CA3-488D Directory of C:\Users\shiresha\Desktop\geeks_dir 21-05-2022 20:31 <DIR> . 21-05-2022 20:31 <DIR> .. 05-03-2022 14:30 <DIR> question1 17-03-2022 21:53 <DIR> question10 22-03-2022 15:24 <DIR> question11 25-03-2022 13:49 <DIR> question12 22-03-2022 17:59 <DIR> question13 25-03-2022 13:52 <DIR> question14 07-04-2022 21:51 <DIR> question15 20-04-2022 08:59 <DIR> question16 04-05-2022 09:24 <DIR> question17 29-04-2022 17:28 <DIR> question18 04-05-2022 09:24 <DIR> question19 24-02-2022 20:23 <DIR> question2 02-05-2022 07:18 <DIR> question20 08-05-2022 13:23 <DIR> question21 07-05-2022 20:47 <DIR> question22 08-05-2022 14:30 <DIR> question23 12-05-2022 20:31 <DIR> question24 21-05-2022 19:51 <DIR> question25 23-02-2022 22:42 <DIR> question3 05-03-2022 15:36 <DIR> question4 07-03-2022 09:51 <DIR> question5 07-03-2022 14:38 <DIR> QUESTION6 12-03-2022 10:18 <DIR> question7 12-03-2022 11:18 <DIR> question8 13-03-2022 14:24 <DIR> question9 09-05-2022 08:59 <DIR> temp 1 File(s) 355 bytes 28 Dir(s) 41,057,894,400 bytes free
Summary
We learned about four different ways to run a external program or a system command in Python. Happy Coding.