We earn commission when you buy through affiliate links.

This does not influence our reviews or recommendations.Learn more.

If you are using Linux, then you would definitely love the shell commands.

And if you are working with Python, then you may have tried to automate things.

Thats a way to save time.

You may also have some bash scripts to automate things.

Pythonis handy to write scripts than bash.

And managing Python scripts are easy compared to bash scripts.

You will find it difficult to maintain the bash scripts once its growing.

But what if you already have bash scripts that you want to run using Python?

Is there any way to execute the bash commands and scripts in Python?

Yeah, Python has a built-in module calledsubprocesswhich is used to execute the commands and scripts inside Python scripts.

Lets see how to execute bash commands and scripts in Python scripts in detail.

It provides different methods and classes for the same.

There are mainly one method and one class to know about from thesubprocessmodule.

These two help us to execute the bash commands in Python scripts.

Lets see them one by one.

subprocess.run()

The methodsubprocess.run()will take a list of strings as a positional argument.

This is mandatory as it has the bash command and arguments for it.

Lets see a quick example.

The above script list all the items in the current working directory as the script lies.

There are no arguments to the command in the above script.

We have given only the bash command.

We can provide additional arguments to thelscommand like-l,-a,-la, etc.

Lets see a quick example with command arguments.

The above command displays all the files including hidden files along with the permissions.

We have provided the argumentlawhich displays files and directories extra information and hidden files.

We may end up making some mistakes while writing the commands.

Errors will raise according to the mistakes.

What if you want to capture them and use them later?

Yeah, we can do that using the keyword argumentstderr.

Lets see an example.

verify you dont have the file with the namesample.txtin the working directory.

The value to the keyword argumentstderrisPIPEwhich helps to return the error in an object.

We can access it later with the same name.

And the keyword argumenttexthelps to tell that the output should be a string.

Similarly, we can capture the output of the command using thestdoutkeyword argument.

subprocess.run() input

you might give input to the commands using theinputkeyword argument.

We will give inputs in a string format.

So, we need to set the keyword argumenttexttoTrue.

By default, it takes it in bytes.

Lets look at an example.

In the above program, the Python scriptadd.pywill take two numbers as input.

We have given the input to the Python script using theinputkeyword argument.

subprocess.Popen()

The classsubprocess.Popen()is advanced than the methodsubprocess.run().

It gives us more options to execute the commands.

Lets see them one by one along with the code examples.

It is used to wait until the completion of the execution of the command.

Lets see the example.

trigger the above codeand observe the output.

You will see that the messageCompleted!is printed before the execution of the command.

We can avoid it using thewaitmethod.

Lets wait till the completion of the command.

If you see the output for the above code, then you will realize thatwaitis actually working.

The print statement is executed after the completion of the command execution.

The methodcommunicateis used to get the output, error and give input to the command.

It returns a tuple containing output and error respectively.

Lets see an example.

We cant pass the input to the classPopendirectly.

We need to use the keyword argument calledstdinto give the input to the command.

The instance of the classPopenwill provide usstdinobject.

It has a method calledwritewhich is used to give the input to the command.

As we discussed earlier, it will take inputs as bytes-like objects by default.

So, dont forget to set the keyword argumenttexttoTruewhile creating the instance ofPopen.

The methodpollis used to check whether the execution of the command is completed or not.

This method will returnNoneif the command is still executing.

Lets see an example.

In the above code, we have used thepingcommand with 5 requests.

There is an infinite loop that iterates until the completion of the command execution.

We have used the methodpollto check the status of the command execution.

If the methodpollreturns code other thanNone, then the execution completes.

And the infinite loop breaks.

Executing Bash Scripts

We have seen two ways to execute the commands.

Now, lets see how to execute the bash scripts in Python scripts.

Thesubprocesshas a method calledcall.

This method is used to execute the bash scripts.

The method returns the exit code from the bash script.

The default exit code for the bash scripts is0.

Lets see an example.

Create a bash script with the name practice.sh as follows.

Now, write a Python script execute the above bash script.

You will get the following output once you initiate the above Python script.

Conclusion

We have seen how to execute bash commands and scripts in Python.

you’re able to use them to automate things more efficiently.