yaoshinga
yaoshinga

Reputation: 57

subprocess.CalledProcessError... returns non zero exit status 2

I am making a python script to execute a shell command and then process the output. 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:

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.

Upvotes: 0

Views: 5399

Answers (2)

ergesto
ergesto

Reputation: 397

Wich key do you want to get ?? bellow is an example using 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"]

Upvotes: 0

Aprillion
Aprillion

Reputation: 22330

missing space in awk-F command because of missing comma in 'awk' '-F'

can you use a single string instead of an array? that might be less error prone IMHO

subprocess.check_output('curl https://...')

Upvotes: 1

Related Questions