Executing bash command using Python os module
Using os module also, we can execute bash command. Here, we have executed a bash command to find the current date.
Python3
import os os.system( 'date +"Today is: %A %d %B"' ) |
Output:
Today is: Friday 02 September
How to run bash script in Python?
If you are using any major operating system, you are indirectly interacting with bash. If you are running Ubuntu, Linux Mint, or any other Linux distribution, you are interacting with bash every time you use the terminal. Suppose you have written your bash script that needs to be invoked from python code. The two common modules for interacting with the system terminal are os and subprocess module.
Let’s consider such a simple example, presenting a recommended approach to invoking subprocesses. As an argument, you have to pass the command you want to invoke and its arguments, all wrapped in a list.
Contact Us