hello,
iam a newbie to python, i am trying to execute a shell command within python code and need the output of the shell command to be passed to the same python code for subsequent use. Here i need the value of the string "ListenPort" from the text file /tmp/file.txt to be stored in a variable. i will use that variable in the same pythin code.
cat /tmp/file.txt
output
NativeVersionEnabled=true
ListenPort=9999
LogToStderr=true
SecureListener=true
ListenBacklog=50
here is the python code
import sys
import os
import subprocess
nm=subprocess.check_output("cat /tmp/file.txt | grep ListenPort | awk -F= '{print $2}'", shell=True)
print nm
Error
File "/local/mnt/apps/ora/product/Middleware/user_projects/domains/bifoundation_domain/test1.py", line 4, in ?
ImportError: no module named subprocess\
/usr/bin/python -- output
Python 2.4.3 (#1, Jun 11 2009, 14:09:37)
[GCC 4.1.2 20080704 (Red Hat 4.1.2-44)] on linux2
Could some body please help me with this code, I am not sure why it says no module subprocess. If there is any other way to do the same thing i am fine with that as well.
i tried doing this as well
cmd_str1="nm=cat /tmp/file.txt | grep ListenPort | awk -F" + "=" + " '{print $2}'
"
os.system(cmd_str1)
print nm
this throws Name Error nm.
where cat /tmp/file.txt gives me this output
ListenPort=9999
SecureListener=true
ListenBacklog=50
how can I the output from a shell command be assigned as a value to a variable in python.
I kindly request somebody to help me with this piece of code.
thanks
RR