How To Execute Shell Commands With Python - Parametric Thoughts
How To Execute Shell Commands With Python - Parametric Thoughts
Parametric Thoughts
import os
os.system('ls -l')
If you save this as a script and run it, you will see the
output in the command line. The problem with this
approach is in its in exibility since you can’t even get the
resulting output as a variable. You can read more about
this function in the documentation.
import os
stream = os.popen('echo Returned output')
output = stream.read()
output
'Returned output\n'
When you use the .read() function, you will get the
whole output as one string. You can also use the
.readlines() function, which splits each line (including
a trailing \n ). Note, that you can run them only once. It
is also possible to write to the stream by using the
mode='w' argument. To delve deeper into this function,
have a look at the documentation.
import subprocess
process = subprocess.Popen(['echo', 'More output'],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
stdout, stderr = process.communicate()
stdout, stderr
https://janakiev.com/blog/python-shell-commands/ 3/7
6/22/2020 How to Execute Shell Commands with Python - Parametric Thoughts
while True:
output = process.stdout.readline()
print(output.strip())
# Do something else
return_code = process.poll()
if return_code is not None:
print('RETURN CODE', return_code)
# Process has finished, read rest of the output
for output in process.stdout.readlines():
print(output.strip())
break
import shlex
shlex.split("/bin/prog -i data.txt -o \"more data.txt\"")
https://janakiev.com/blog/python-shell-commands/ 4/7
6/22/2020 How to Execute Shell Commands with Python - Parametric Thoughts
process.stdout
import subprocess
# Fetch output
for line in ssh.stdout:
print(line.strip())
Here you can see how to write input to the process. In this
case you need to set the bufsize=0 in order to have
unbuffered output. After you are nished writing to the
stdin , you need to close the connection.
https://janakiev.com/blog/python-shell-commands/ 5/7
6/22/2020 How to Execute Shell Commands with Python - Parametric Thoughts
Conclusion
You have seen now how to run external commands in
Python. The most effective way is to use the subprocess
module with all the functionality it offers. Most notably,
you should consider using subprocess.run . For a short
and quick script you might just want to use the
os.system() or os.popen() functions. If you have any
questions, feel free to leave them in the comments below.
There are also other useful libraries that support shell
commands in Python, like plumbum, sh, psutils and
pexpect.
Further Reading
4 Techniques for Testing Python Command-Line
(CLI) Apps
Frequently Used Arguments - subprocess
Related Posts
How to Manage Apache Airflow with Systemd on
Debian or Ubuntu
20 Dec 2019
https://janakiev.com/blog/python-shell-commands/ 6/7
6/22/2020 How to Execute Shell Commands with Python - Parametric Thoughts
https://janakiev.com/blog/python-shell-commands/ 7/7