Home » Python

How to call an external command in Python?

Calling extern command in Python: Here, we are going to learn how to call an external command in Python programing language?
Submitted by Sapna Deraje Radhakrishna, on October 20, 2019

In order to run an external command within python env, or start up a daemon process, or perhaps invoke a command and capture its output, Python supports several methods for invoking an external command.

Choosing the method depends upon the requirement if there is a need to retain the output from the command, or send an input to the command, or have control over its lifecycle.

Subprocess

Subprocess is the recommended module in python (version 3.5 +) to execute an external command. Using the call method of the subprocess module, an external command can be invoked within the python function.

Python 3.6.8 (default, Apr 25 2019, 21:02:35)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-36)] on linux
Type "help", "copyright", "credits" or "license" for more information.

>>> import subprocess
>>> subprocess.call(['ls', '-l'])
total 8
drwxr-xr-x 4 XXXXX sw 4096 Oct  1 17:37 python_samples
drwxr-xr-x 5 XXXXX sw 4096 Sep 29 19:53 venv
0
>>>

Re-directing the output to the file

In order to redirect the output to a file, using the with function to open the file and use stdout parameter.

Python 3.6.8 (default, Apr 25 2019, 21:02:35)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-36)] on linux
Type "help", "copyright", "credits" or "license" for more information.

>>> import subprocess
>>> with open('output.txt', 'w') as f:
...     subprocess.call(['whoami'], stdout=f)
...
>>> exit()
-bash-4.2$ more output.txt
User

Passing an argument as input from stdin to command

Using the call() . Open a file and pass the handle using the stdin parameter.

-bash-4.2$ python3
Python 3.6.8 (default, Apr 25 2019, 21:02:35)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-36)] on linux
Type "help", "copyright", "credits" or "license" for more information.

>>> import subprocess
>>> with open('output.txt', 'r') as f:
...     subprocess.call(['more'], stdin =f)
...
user
>>>

Using shell=True , a shell command can be passed to the call() function.

-bash-4.2$ python3
Python 3.6.8 (default, Apr 25 2019, 21:02:35)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-36)] on linux
Type "help", "copyright", "credits" or "license" for more information.

>>> import subprocess
>>> subprocess.call('ls -la', shell=True)
total 20
drwxr-xr-x 4 sradhakr sw 4096 Oct 17 15:42 .
drwxr-xr-x 8 sradhakr sw 8192 Oct  8 13:40 ..
-rw-r--r-- 1 sradhakr sw    9 Oct 17 15:42 output.txt
drwxr-xr-x 4 sradhakr sw 4096 Oct  1 17:37 python_samples
drwxr-xr-x 5 sradhakr sw 4096 Sep 29 19:53 venv
>>>

Using shell=True, comes with a security risk if the command comes from an untrusted source. For example, using the user input, if we receive a command such as rm –rf/ is potentially a dangerous command for a file system.

In order to capture the command output in a string, use subprocess.check_output(),

import subprocess

try:
    output = subprocess.check_output(input('enter command >> '), shell=True)
    print(output)
except subprocess.CalledProcessError as e:
    print('exception with returncode: {}, command: {}, output = {}'.format(e.returncode, e.cmd, e.output))

Output

enter command >> whoami
b'user\n'

Exit code from the command

The exit command determines the success or failure of the processing of the command. The return value of the call() is the exit code and it determines the result of the processing of the command.

Python 3.6.8 (default, Apr 25 2019, 21:02:35)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-36)] on linux
Type "help", "copyright", "credits" or "license" for more information.

>>> import subprocess
>>> print("return code {}".format(subprocess.call('ls -la', shell=True)))
total 20
drwxr-xr-x 4 sradhakr sw 4096 Oct 17 15:42 .
drwxr-xr-x 8 sradhakr sw 8192 Oct  8 13:40 ..
-rw-r--r-- 1 sradhakr sw    9 Oct 17 15:42 output.txt
drwxr-xr-x 4 sradhakr sw 4096 Oct  1 17:37 python_samples
drwxr-xr-x 5 sradhakr sw 4096 Sep 29 19:53 venv
return code 0

In order to get additional information about the processing of the external command there is an additional method called subprocess.check_call().

import subprocess
try:
    print(subprocess.check_call(['cat','random.txt']))
except subprocess.CalledProcessError as e:
     print('exception: returncode: {}, command: {}'.format(e.returncode, e.cmd))

Output

cat: random.txt: No such file or directory
exception: returncode: 1, command: ['cat', 'random.txt']





Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.




Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates


© https://www.includehelp.com some rights reserved.