Executing shell commands in python
Executing shell commands in python : When working with python, it is sometimes necessary to execute shell/bash codes directly from the python script.In this article, I’m going to show you several methods to do this directly from its python code.
The simplest way: Using the OS module
The first method to execute a shell command is to use the os.system () function:
import os os.system('mkdir amiradata')
You can run this script and then you will see the result of the command in the terminal. The main problem with this command is that the result can’t be stored in a variable in the python code suite which can be annoying when you want to retrieve the list of a folder for example.
There is a function in the bone module that allows the result to be stored as a variable. The function os.popen() opens a channel from or to the command line. This means that we can access the stream in Python.
import os stream = os.popen('echo AMIRADATA') output = stream.read() output
With this function you get the result: “AMIRADATA”. That’s great. 🙂
The best way : Using the subprocess module
This is the most versatile method and the recommended module for executing external commands in Python:
import subprocess process = subprocess.Popen(['echo', 'AMIRADATA'], stdout=subprocess.PIPE, stderr=subprocess.PIPE) stdout, stderr = process.communicate() stdout, stderr
You will notice that the output is of byte type . You can solve this problem by typing stdout.decode(‘utf-8’) or by adding universal_newlines=True when calling subprocess.Popen.
We have seen how to execute external commands in Python. The most efficient and versatile way is to use the subprocess module, it offers a lot of features that we have not listed in this mini tutorial, I advise you to consult the documentation (Subprocess module python) . For a short and fast script, you can simply use the os.system() or os.popen() functions.
If you have any questions or need clarification on any of these modules, please do not hesitate to leave a comment. I would be happy to help you! 🙂
Have a look at the Learning section if you enjoyed the tutorial “Executing shell commands in python”.
See you soon for a new tutorial !