w3resource

Python: Call an external command

Python Basic: Exercise-45 with Solution

Write a Python program that calls an external command.

Sample Solution-1:

Python Code:

# Import the 'call' function from the 'subprocess' module.
from subprocess import call

# Use the 'call' function to execute the "ls -l" command.
# This command lists the files and directories in the current directory with details.
call(["ls", "-l"])

Sample Output:

drwxrwxr-x 2 students  students    4096 Sep 15  2016 c48d5200-7b43-11e6-b7e4-2516b8e1f7f8                     
drwxrwxr-x 2 students  students    4096 Sep 21  2016 c4a96850-7fdf-11e6-a2b3-172481646809                     
drwxrwxr-x 2 students  students    4096 Sep  7  2016 c4d98410-74de-11e6-ac36-65f2f75d0c7b                     
drwxrwxr-x 2 students  students    4096 Sep  6  2016 c4fa6e10-7424-11e6-812c-5574d751e2d7                     
drwxrwxr-x 2 prashanta prashanta   4096 Feb 20 18:24 c5216400-f76b-11e6-8ad1-9b7f6e755728   
-------
-rw-rw-r-- 1 students  students      27 Sep 28  2016 temp.txt                                                 
-rw-rw-r-- 1 students  students      27 Sep 28  2016 test.txt  

Explanation:

In Python The subprocess module allows us to spawn new processes, connect to their input/output/error pipes, and obtain their return codes. This module intends to replace several older modules and functions:

The above Python code uses the ‘subprocess’ module to execute a command in the shell. Then, it calls the call function from the subprocess module with a list of arguments as its parameter.

When the call function is executed, it will execute the specified command in the shell and wait for it to complete before returning.

So, when the above code is executed, it will print the long format list of files in the current directory on the console output, just like running the ls -l command directly in the terminal.

Sample Solution-2:

Python Code:

# Import the 'os' module to work with the operating system.
import os

# Use 'os.system(command)' to execute the 'ls -l' command in the system's shell.
# This command lists the files and directories in the current directory and prints the result.
print(os.system('ls -l'))

Sample Output:

total 4
-rw-rw-rw- 1 root root 36 Jun  9 10:47 main.py
0 

Explanation:

The ‘OS’ module provides a portable way of using operating system dependent functionality.

The above Python code imports the os module and then calls its system function to execute a shell command.

When the system function is called, it executes the specified command in the shell. In this case, the command is ls -l, which is executed in the shell.

Finally, the code calls print() function to output the return value of the system function.

Sample Solution-3:

Python Code:

# Import the 'psutil' module for system monitoring, including CPU information.
import psutil

# Use 'psutil.cpu_count()' to get the number of available CPU cores.
# This function returns the number of logical CPU cores in the system.
print(psutil.cpu_count())

Sample Output:

4

Explanation:

psutil (process and system utilities) is a cross-platform library for retrieving information on running processes and system utilization (CPU, memory, disks, network, sensors) in Python. It is useful mainly for system monitoring, profiling and limiting process resources and management of running processes.

In the above Python code we first import the ‘psutil’ module and then calls its cpu_count() function to get the number of CPU cores on the current system.

When the cpu_count() function is called, it returns the number of CPU cores on the system. Finally the code calls print() function to output the number of CPU cores on the system to the console output.

Python Code Editor:

 

Previous: Write a Python program to locate Python site-packages.
Next: Write a python program to get the path and name of the file that is currently executing.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://198.211.115.131/python-exercises/python-basic-exercise-45.php