-1

I need to get the input from a Bash command and store it as a Python variable (sprice; a single float). On Python 2.7 the 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 we have to 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 the float we're after, enclosed by brackets.

['40.365']

Which is all right if I want to see the output and be done. But I need to store it in a Python variable like in the previous (2.7) case. However when I try to assign it to a variable I 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 is the proper way to do this?

Calculus Knight
  • 113
  • 1
  • 12
  • 1
    version of check_output that will work with python2,6 http://stackoverflow.com/questions/29580663/save-error-message-of-subprocess-command/29580739#29580739 – Padraic Cunningham Apr 24 '15 at 23:21
  • related: [Running shell command from python and capturing the output](http://stackoverflow.com/q/4760215/4279) – jfs Apr 25 '15 at 22:49

2 Answers2

-1
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 the string cmd in a shell with os.popen() and return a 2-tuple (status, output). cmd is actually run as { cmd ; } 2>&1, so that the returned output will contain output or error messages.

enigma
  • 3,476
  • 2
  • 17
  • 30
-1

My syntax was wrong. This Q&A gets it straight.

Subprocess Popen and PIPE in Python

So the command is:

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

Community
  • 1
  • 1
Calculus Knight
  • 113
  • 1
  • 12
  • the code in your question should work as is i.e., you can write `p = Popen(...)` and later `sprice = p.communicate()[0]`. What syntax error exactly may lead to `ValueError: I/O operation on closed file`? You have to call `proc.output.close()` to cause the error. – jfs Apr 25 '15 at 22:59
  • I don't know, but the error appears, nonetheless. With this syntax everything goes well and the objective is achieved. – Calculus Knight Apr 26 '15 at 08:20
  • I don't see any difference that matters. Could you provide a [standalone example that causes ValueError](http://stackoverflow.com/help/mcve)? – jfs Apr 26 '15 at 15:08
  • No need. This works as intended. Isn't the information in the question enough to reproduce this? – Calculus Knight Apr 27 '15 at 15:54