[英]subprocess.CalledProcessError… returns non zero exit status 2
I am making a python script to execute a shell command and then process the output. 我正在制作一个python脚本来执行shell命令,然后处理输出。 I want to execute this command:
我要执行以下命令:
curl "https://api.github.com/users/username/repos?per_page=200" | grep -o 'git@[^"]*'| awk -F "/" '{print $2}'| awk -F "." '{print $1}'
I am using subprocess.check_output
method something like: 我正在使用
subprocess.check_output
方法,例如:
with open(os.devnull,'w') as devnull:
f=subprocess.check_output(['curl', 'https://api.github.com/users/username/repos?per_page=200', '|', 'grep', '-o', 'git@[^"]*','|', 'awk' ,'-F' ,'/', '{print $2}' ,'|' ,'awk', '-F', '.' ,'{print $1}'],stderr=devnull)
res=ujson.loads(f)
data=res.get('items')
print(data[0].get('login'))
But it gives the following Error: 但是它给出了以下错误:
subprocess.CalledProcessError: Command '['curl', 'https://api.github.com/users/username/repos?per_page=200', '|', 'grep', '-o', 'git@[^"]*', '|', 'awk-F', '/', '{print $2}', '|', 'awk', '-F', '.', '{print $1}']' returned non-zero exit status 2
I have checked the similar questions but they didn't solve the problem. 我已经检查了类似的问题,但他们没有解决问题。
missing space in awk-F
command because of missing comma in 'awk' '-F'
由于
'awk' '-F'
awk-F
'awk' '-F'
中缺少逗号, awk-F
命令中缺少空格
can you use a single string instead of an array? 您可以使用单个字符串代替数组吗? that might be less error prone IMHO
可能不太容易出错恕我直言
subprocess.check_output('curl https://...')
Wich key do you want to get ?? 你想得到钥匙? bellow is an example using urllib2 & json
下面是使用urllib2和json的示例
import urllib2 , json
data = json.load(urllib2.urlopen("https://api.github.com/users/test/repos?per_page=200"))
for repos in data:
print repos["name"]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.