I’m very new to python, I have a bash code which stores the result of the file into a variable like this:
variable=(`cat text`)
The variable is now has the contents of test. I tried similar in python using Popen call like this:
subprocess.Popen("variable=(`cat text`)")
But I get error like this:
Traceback (most recent call last):
File "./push-jenkins", line 5, in <module>
subprocess.Popen("variable=(`cat text`)")
File "/usr/lib/python2.7/subprocess.py", line 679, in __init__
errread, errwrite)
File "/usr/lib/python2.7/subprocess.py", line 1249, in _execute_child
raise child_exception
OSError: [Errno 2] No such file or directory
Not sure where I’m making the mistake.
Thanks in advance.
You cannot access bash variables from Python. Do instead:
I’m assuming that this command is just an example, as you can read a file from Python easy enough.
Note that the call to
check_outputreceives the command as a split list of arguments, not as a single string. If you have a single string and don’t want do split manually you can do:Or even better:
And let the shell do the hard work.