比较来自世界各地的卖家的域名和 IT 服务价格

为什么这挑战 bash 的 python 不起作用吗?

我有点有点比特币。 当我想获得有关当地堡宾安装的一些信息时,我刚刚运行
bitcoin getinfo

我得到这样的东西:


{
"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" : ""
}


现在我想从里面发出这个电话 Python /在某人表示这个​​之前; 我知道 , 什么是
https://github.com/petertodd/python-bitcoinlib
, 我只是想学习如何自己做到这一点/. 所以我第一次试图履行一个简单的命令
ls

, 类似于这个:


import subprocess
process = subprocess.Popen/'ls', stdout=subprocess.PIPE/
output = process.communicate//[0]
print output


它通过预期打印文件和文件夹列表来工作。 然后我这样做了:


import subprocess
process = subprocess.Popen/'bitcoin getinfo', stdout=subprocess.PIPE/
output = process.communicate//[0]
print output


但它给出以下错误:


Traceback /most recent call last/:
File "testCommandLineCommands.py", line 2, in <module>
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


然后我失败了。 有谁知道这里有什么不对吗? 欢迎所有提示!

[EDIT]
使用以下优秀答案,我现在做了以下功能,可以有用和其他功能。 它接受一个字符串或迭代,单个参数,如果它相等,则分析结论 json:


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


</module>
已邀请:

卫东

赞同来自:

要么在参数中使用序列:


process = subprocess.Popen/['bitcoin', 'getinfo'], stdout=subprocess.PIPE/


或设置参数
shell

价值
True

:


process = subprocess.Popen/'bitcoin getinfo', stdout=subprocess.PIPE, shell=True/


有关更多信息,您可以找到
https://docs.python.org/2/libr ... ments
:

args 所有调用所需的,应该是程序参数的字符串或序列。 提供一系列参数通常是因为它允许模块处理任何所需的屏蔽和引用参数 /例如,允许文件名中的空白/. 转移一行时 shell 必须是 True /见下文/, 字符串应该只需调用正在执行的程序而不指定任何参数。

裸奔

赞同来自:

使用以下代码:


process = subprocess.Popen/['bitcoin', 'getinfo'], stdout=subprocess.PIPE/


您不允许使用空格来传输参数。

要回复问题请先登录注册