# Python - store output of command to a variable

**URL:** <https://community.unix.com/t/python-store-output-of-command-to-a-variable/369333>\
**Category:** Programming\
**Created:** [November 17, 2017, 4:51am UTC](https://community.unix.com/t/python-store-output-of-command-to-a-variable/369333 "2017-11-17T04:51:36Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![atanubanerji](https://community.unix.com/letter_avatar/atanubanerji/32/6_449c533f586939b6e5f246632a46421f.png) [@atanubanerji](https://community.unix.com/u/atanubanerji)\
**Post date:** [November 17, 2017, 4:51am UTC](https://community.unix.com/t/python-store-output-of-command-to-a-variable/369333/1 "2017-11-17T04:51:36Z")

</div>

I am trying to store output of python command in variable. Could you please help how I can do that ?

For example I am executing the following command - `"CentOS" in server_desc`  
The output would be True or False  
I would like to store the output in a variable say outPut and use condition something like -

```nohighlight
if outPut == "True":
    do_something
else:
     do_otherthing

```

I just started learning python - please help/guide me.

---

<div class="post-metadata">

**Author:** ![rbatte1](https://community.unix.com/letter_avatar/rbatte1/32/6_449c533f586939b6e5f246632a46421f.png) [@rbatte1](https://community.unix.com/u/rbatte1)\
**Post date:** [November 17, 2017, 6:49am UTC](https://community.unix.com/t/python-store-output-of-command-to-a-variable/369333/2 "2017-11-17T06:49:18Z")

</div>

Hello atanubanerji,

I have a few to questions pose in response first:-

- What have you tried so far?
- What output/errors do you get?
- What OS and version are you using?
- Beyond Python, what are your preferred tools? (C, shell, perl, awk, etc.)
- What logical process have you considered? (to help steer us to follow what you are trying to achieve)

Most importantly, What have you tried so far? Please share your code so far, wrapped in CODE tags. That way we can see how you are using it and make suggestions.

There are probably many ways to achieve most tasks, so giving us an idea of your style and thoughts will help us guide you to an answer most suitable to you so you can adjust it to suit your needs in future.

We're all here to learn and getting the relevant information will help us all.

Thanks, in advance,  
Robin

---

<div class="post-metadata">

**Author:** ![atanubanerji](https://community.unix.com/letter_avatar/atanubanerji/32/6_449c533f586939b6e5f246632a46421f.png) [@atanubanerji](https://community.unix.com/u/atanubanerji)\
**Post date:** [November 17, 2017, 7:21am UTC](https://community.unix.com/t/python-store-output-of-command-to-a-variable/369333/3 "2017-11-17T07:21:07Z")

</div>

Thank you very much Robin for your help.

I have to execute some action if the server is CentOS.

The server\_description variable may or may not store CentOS key word among few other words. So I was thinking, if I could execute  
`"CentOS in server_description` which would return either "True" or "False". If it is True, the python would perform some action. I have been advised to store the True or False value in another variable.

My problem is I am unable to store True or False in other variable, say, my\_Check.

If I could do that, probably I could use the following -

```nohighlight
if my_check == "True":
    do_something
else:
     do_otherthing

```

Could you please advice, how I can store a command's output, here, `"CentOS in server_description` in a variable (or any other python level command later) and use the value of the variable whenever required.  
e.g.

```nohighlight
server_description = "This is xyz abc of mno CentOS"
my_Check = "CentOS" in server_description
if my_Check == "True":
    print("This is CentOS")
else:
    print("This is NOT CentOS")

```

The output I am getting is "This is NOT CentOS"  
CentOS 7.4 is OS version and Python 3.4.5  
I could do it using shell, but I have been asked to use Python only, as the action to be performed based on the condition, if True or False, is already written in Python.  
Please don't consider my objective is to print either CentOS or NOT CentOS, rather some other successfully written python code that I will be pasting in if or else section.

While testing, I found this, but not sure why it is not working...

```nohighlight
>>> server_description = "CentOS"
>>> my_Check = "centos" in server_description
>>> my_Check
False
>>> my_Check = "CentOS" in server_description
>>> my_Check
True

```

---

<div class="post-metadata">

**Author:** ![apmcd47](https://community.unix.com/letter_avatar/apmcd47/32/6_449c533f586939b6e5f246632a46421f.png) [@apmcd47](https://community.unix.com/u/apmcd47)\
**Post date:** [November 17, 2017, 10:15am UTC](https://community.unix.com/t/python-store-output-of-command-to-a-variable/369333/4 "2017-11-17T10:15:18Z")

</div>

Try this:

```nohighlight
server_description = "This is xyz abc of mno CentOS"
my_Check = "CentOS" in server_description
if my_Check == True:
    print("This is CentOS")
else:
    print("This is NOT CentOS")

```

Don't quote the "True". By quoting the "True" you were comparing a Boolean with a string. But the truth is you don't even need that:

```nohighlight
server_description = "This is xyz abc of mno CentOS"
my_Check = "CentOS" in server_description
if my_Check:
    print("This is CentOS")
else:
    print("This is NOT CentOS")

```

Or even:

```nohighlight
server_description = "This is xyz abc of mno CentOS"
if "CentOS" in server_description:
    print("This is CentOS")
else:
    print("This is NOT CentOS")

```

As for this:

```nohighlight
>>> server_description = "CentOS"
>>> my_Check = "centos" in server_description
>>> my_Check
False
>>> my_Check = "CentOS" in server_description
>>> my_Check
True

```

Python is case-sensitive. "centos" is not "CentOS". If you want a case-insensitive string comparison, try:

```nohighlight
"centos".lower() in server_description.lower() 

```

There may be other ways.

Andrew

---

<div class="post-metadata">

**Author:** ![atanubanerji](https://community.unix.com/letter_avatar/atanubanerji/32/6_449c533f586939b6e5f246632a46421f.png) [@atanubanerji](https://community.unix.com/u/atanubanerji)\
**Post date:** [November 19, 2017, 5:39am UTC](https://community.unix.com/t/python-store-output-of-command-to-a-variable/369333/5 "2017-11-19T05:39:59Z")

</div>

Thank you very much Andrew !! Appreciate your advice.
