Python 2.6: Get input from bash command with Popen and communicate and store as variable -


i need input bash command , store python variable (sprice; single float). on python 2.7 following works well:

bashcommand = "curl -s 'http://download.finance.yahoo.com/d/quotes.csv?s=vwrl.as&f=l1'" sprice = float(subprocess.check_output(bashcommand, shell=true)) 

however on python 2.6 check_output isn't available. instead have use:

proc = popen(['curl', '-s', 'http://download.finance.yahoo.com/d/quotes.csv?s=vwrl.as&f=l1'], stdout=pipe) print (proc.communicate()[0].split()) 

which shows float we're after, enclosed brackets.

['40.365'] 

which right if want see output , done. need store in python variable in previous (2.7) case. when try assign variable get:

traceback (most recent call last):      file "arr.py", line 49, in <module>     sprice = proc.communicate()[0].split()   file "/usr/lib/python2.7/subprocess.py", line 791, in communicate     stdout = _eintr_retry_call(self.stdout.read)   file "/usr/lib/python2.7/subprocess.py", line 476, in _eintr_retry_call     return func(*args) valueerror: i/o operation on closed file 

which proper way this?

import commands status, output = commands.getstatusoutput("curl -s http://download.finance.yahoo.com/d/quotes.csv?s=iwda.as&f=l1") 

from the docs:

execute string cmd in shell os.popen() , return 2-tuple (status, output). cmd run { cmd ; } 2>&1, returned output contain output or error messages.


Comments

Popular posts from this blog

shopping cart - Page redirect not working PHP -

php - How to modify a menu to show sub-menus -

python - Installing PyDev in eclipse is failed -