title: Why does this bash call from python not work?
body: <p>I am messing around with Bitcoins a bit. When I want to get some info about the local bitcoin install, I simply run <code>bitcoin getinfo</code> and I get something like this:</p>
  
  <pre><code>{
      "version" : 90100,
      "protocolversion" : 70002,
      "walletversion" : 60000,
      "balance" : 0.00767000,
      "blocks" : 306984,
      "timeoffset" : 0,
      "connections" : 61,
      "proxy" : "",
      "difficulty" : 13462580114.52533913,
      "testnet" : false,
      "keypoololdest" : 1394108331,
      "keypoolsize" : 101,
      "paytxfee" : 0.00000000,
      "errors" : ""
  }
  </code></pre>
  
  <p>I now want to do this call from within Python (before anyone points it out; I know there are <a href="https://github.com/petertodd/python-bitcoinlib" rel="nofollow">Python implementations for Bitcoin</a>, I just want to learn doing it myself). So I first tried executing a simple <code>ls</code> command like this:</p>
  
  <pre><code>import subprocess
  process = subprocess.Popen('ls', stdout=subprocess.PIPE)
  output = process.communicate()[0]
  print output
  </code></pre>
  
  <p>This works fine, printing out the list of files and folders as expected. So then I did this:</p>
  
  <pre><code>import subprocess
  process = subprocess.Popen('bitcoin getinfo', stdout=subprocess.PIPE)
  output = process.communicate()[0]
  print output
  </code></pre>
  
  <p>but this gives the following error:</p>
  
  <pre><code>Traceback (most recent call last):
    File "testCommandLineCommands.py", line 2, in &lt;module&gt;
      process = subprocess.Popen('bitcoin getinfo', stdout=subprocess.PIPE)
    File "/usr/lib/python2.7/subprocess.py", line 710, in __init__
      errread, errwrite)
    File "/usr/lib/python2.7/subprocess.py", line 1327, in _execute_child
      raise child_exception
  OSError: [Errno 2] No such file or directory
  </code></pre>
  
  <p>and here I'm kinda lost. Does anybody know what's wrong here? All tips are welcome!</p>
  
  <p>[EDIT]
  Using the excellent answers below I now made the following function, which might come in handy for others as well. It takes either a string, or an iterable with separate arguments and parses the output if it is json:</p>
  
  <pre><code>def doCommandLineCommand(command):
      process = subprocess.Popen(command, stdout=subprocess.PIPE, shell=isinstance(command, str))
      output = process.communicate()[0]
      try:
          return json.loads(output)
      except ValueError:
          return output
  </code></pre>