7 Replies - 966 Views - Last Post: 13 October 2012 - 08:37 PM Rate Topic: -----

#1 rr8  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 12-October 12

output from a shell command be assigned as a value to a variable in py

Posted 12 October 2012 - 11:11 AM

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

This post has been edited by atraub: 12 October 2012 - 06:31 PM
Reason for edit:: Added code tags


Is This A Good Question/Topic? 0
  • +

Replies To: output from a shell command be assigned as a value to a variable in py

#2 Eric115  Icon User is offline

  • coderさん
  • member icon

Reputation: 63
  • View blog
  • Posts: 696
  • Joined: 19-January 09

Re: output from a shell command be assigned as a value to a variable in py

Posted 12 October 2012 - 06:25 PM

Hi,

It is hard to see why you are getting the import error (code tags!!),
but if you would like to try an alternate method, try something like this:
result=os.popen('echo your_command').read()
print result


I hope that helps!

This post has been edited by Eric115: 12 October 2012 - 06:26 PM

Was This Post Helpful? 1
  • +
  • -

#3 rr8  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 12-October 12

Re: output from a shell command be assigned as a value to a variable in py

Posted 13 October 2012 - 12:03 AM

hi eric,

it worked like a charm,

thanks a ton
rr
Was This Post Helpful? 0
  • +
  • -

#4 Eric115  Icon User is offline

  • coderさん
  • member icon

Reputation: 63
  • View blog
  • Posts: 696
  • Joined: 19-January 09

Re: output from a shell command be assigned as a value to a variable in py

Posted 13 October 2012 - 04:55 AM

No problem, glad I could help :)
Was This Post Helpful? 0
  • +
  • -

#5 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5874
  • View blog
  • Posts: 12,754
  • Joined: 16-October 07

Re: output from a shell command be assigned as a value to a variable in py

Posted 13 October 2012 - 05:01 AM

Don't do that.

Use the language you have, not the OS. You want to read a file and pick data out of it? Any language will do that.

In Python it's cake:
>>> with open("file.txt") as fh:
...     data = fh.readlines()
... 
>>> data
['NativeVersionEnabled=true\n', 'ListenPort=9999\n', 'LogToStderr=true\n', 'SecureListener=true\n', 'ListenBacklog=50\n']
>>> data = [ i.strip().split('=') for i in data ]
>>> data
[['NativeVersionEnabled', 'true'], ['ListenPort', '9999'], ['LogToStderr', 'true'], ['SecureListener', 'true'], ['ListenBacklog', '50']]
>>> data = dict( i for i in data if len(i)==2)
>>> data
{'SecureListener': 'true', 'LogToStderr': 'true', 'ListenPort': '9999', 'NativeVersionEnabled': 'true', 'ListenBacklog': '50'}
>>> data['ListenPort']
'9999'
>>> 


Was This Post Helpful? 1
  • +
  • -

#6 atraub  Icon User is offline

  • Pythoneer
  • member icon

Reputation: 759
  • View blog
  • Posts: 2,010
  • Joined: 23-December 08

Re: output from a shell command be assigned as a value to a variable in py

Posted 13 October 2012 - 06:56 AM

yo dawg! I heard you like list comprehensions. I also heard you like dictionary comprehensions. So we put a list comprehension in yo dictionary comprehension so you can comprehend while you comprehend!
data = dict(i for i in [ i.strip().split('=') for i in data ] if len(i) == 2)


I'm in a weird mood this morning...

This post has been edited by atraub: 13 October 2012 - 06:57 AM

Was This Post Helpful? 0
  • +
  • -

#7 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5874
  • View blog
  • Posts: 12,754
  • Joined: 16-October 07

Re: output from a shell command be assigned as a value to a variable in py

Posted 13 October 2012 - 07:50 AM

Well, I really wanted to show the steps so it was clear what was going on.

If you really wanted to be clever about it:
def getConfig(filename):
    with open(filename) as fh:
        return dict( j for j in ( i.strip().split('=') for i in fh.readlines() ) if len(j)==2)



Note that generators are use throughout. None of that wasted list crap. :P
Was This Post Helpful? 1
  • +
  • -

#8 atraub  Icon User is offline

  • Pythoneer
  • member icon

Reputation: 759
  • View blog
  • Posts: 2,010
  • Joined: 23-December 08

Re: output from a shell command be assigned as a value to a variable in py

Posted 13 October 2012 - 08:37 PM

good idea!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1