All TalkersCode Topics

Follow TalkersCode On Social Media

devloprr.com - A Social Media Network for developers Join Now ➔

Python Shell Command Output To Variable

Last Updated : Mar 11, 2024

Python Shell Command Output To Variable

In this tutorial we will show you the solution on python shell command output to variable, while making a program in Python, you may need to execute some shell commands for your program.

For example, if you use the Pycharm IDE, you can share your project on GitHub.

And you probably know that file transferring is done by git, which is operated using a command line.

So, Pycharm executes some shell commands in the background to do it. So, let’s get into it.

Step By Step Guide On Python Shell Command Output To Variable :-

This can be very difficult to perform without the built-in function provided by Python.

Python provides a “subprocess.check_output()” function that you can use in your program to get the output of a shell command into a variable.

import subprocess
cmd1 = "date"
returned_output1 = subprocess.check_output(cmd1)
print(returned_output1.decode())
cmd2 = "ls"
returned_output2 = subprocess.check_output(cmd2)
print(returned_output2.decode())
  1. We will use the “subprocess” module throughout this program to achieve the desired result.
  2. So in the first line there is an import statement that imports the “subprocess” module in the program.
  3. In the next line there is a variable named “cmd1”, which means command number 1 that has a string value of “date”. The string “date” is a shell command.
  4. In the next line, there is a variable named “returned_output1” which will store the value returned from the “subprocess.check_output()” function. It has the variable “cmd1” passed in as an argument.
  5. By default, the “subprocess.check_output()” function will return the data as encoded bytes. The actual encoding of the output data may depend on the command being invoked, so the decoding to text will often need to be handled at the application level.
  6. With the help of the "decode" function, the next line contains a print statement that will print the decoded value of the variable "returned_output1".
  7. The Python bytes decode() function is used to convert bytes to string objects. In this case, it will decode the byte stored in the variable “returned_output1” and, after that, the print statement will display the output of the command “date”.
  8. The print statement will display “Tue 23 Aug 2022 04:55:14 PM UTC” or you can say that it will display the current timestamp in the output window.
  9. Now in the second example, we can use some different commands whose output needs to be stored in a variable.
  10. In the next line there is another variable named “cmd2”, which means that the command number 2 has a value of “ls” assigned to it.
  11. In the next line, there is a variable named “retunred_output2” that will store the value returned from the “subprocess.check_output()” function.
  12. Now, this time the argument passed in is the value of the variable “cmd2”, which is the “ls” command.
  13. In the last line, there is a print statement that will display the decoded value of the variable “returned_output2” in the output window.

Conclusion :-

So finally, in conclusion, we can say that with the help of this article, you can now get the output of a shell command in any variable in Python.

You just have to pass the command as the argument of the “subprocess.check_output()” function and it will do the work for you.

I hope this tutorial on python shell command output to variable helps you and the steps and method mentioned above are easy to follow and implement.

Author Image About Dikshita

Passionate Electronics and Communication Engineering student with expertise in web development (HTML, CSS, JS,PHP, Bootstrap, React.js) and content writing. Eager problem solver and tech enthusiast, adept at creating engaging web experiences.

Follow Dikshita On Linkedin 🡪