简体   繁体   English

subprocess.CalledProcessError…返回非零退出状态2

[英]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.

相关问题 subprocess.CalledProcessError: ...返回非零退出状态 255 - subprocess.CalledProcessError: ... returned non-zero exit status 255 Python subprocess.CalledProcessError:返回非零退出状态 2 - Python subprocess.CalledProcessError: returned non-zero exit status 2 subprocess.CalledProcessError:返回非零退出状态0 - subprocess.CalledProcessError: returned non-zero exit status 0 subprocess.CalledProcessError返回非零退出状态1 - subprocess.CalledProcessError returned non-zero exit status 1 "subprocess.CalledProcessError:命令“df”返回非零退出状态 1" - subprocess.CalledProcessError: Command 'df' returned non-zero exit status 1 Python subprocess.CalledProcessError:命令'adb设备'返回非零退出状态127 - Python subprocess.CalledProcessError: Command 'adb devices' returned non-zero exit status 127 Python 3.9.6 - subprocess.run - subprocess.CalledProcessError:命令 pip 安装 pkg 返回非零退出状态 1 - Python 3.9.6 - subprocess.run - subprocess.CalledProcessError: Command pip install pkg returned non-zero exit status 1 运行Python Cookiecutter以从GitHub中获取模板的结果为subprocess.CalledProcessError,返回非零退出状态128 - Running Python Cookiecutter to get template from GitHub results in subprocess.CalledProcessError, returned non-zero exit status 128 Python:wifi subprocess.CalledProcessError:命令'['/ sbin / ifdown','wlp4s0']'返回非零退出状态1 - Python: wifi subprocess.CalledProcessError: Command '['/sbin/ifdown', 'wlp4s0']' returned non-zero exit status 1 subprocess.CalledProcessError:返回非零退出状态 1,而 os.system 不会引发任何错误 - subprocess.CalledProcessError: returned non-zero exit status 1, while os.system does not raise any error
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM