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 -
if outPut == "True":
I just started learning python - please help/guide me.
Last edited by rbatte1; 11-17-2017 at 11:46 AM..
Reason: Changed HTML to CODE or ICODE tags
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.
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 -
if my_check == "True":
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.
server_description = "This is xyz abc of mno CentOS"
my_Check = "CentOS" in server_description
if my_Check == "True":
print("This is CentOS")
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...