I got a very strange behaviour with subprocess.check_out
:
I want to get the grep -c
result:
In [50]: !grep -c 'def ' repos/clara.aaa_hotmail.com/*.py
0
In [52]: !grep -c 'def ' repos/saad.aaa_gmail.com/*.py
3
In [53]:
with this one I get it well
In [53]: from subprocess import check_output
In [54]: check_output([f"grep -c 'def ' repos/saad.aaa_gmail.com/*.py"], shell=True)
Out[54]: b'3\n'
with the other:
In [55]: check_output([f"grep -c 'def ' repos/clara.aaa_hotmail.com/*.py"], shell=True)
---------------------------------------------------------------------------
CalledProcessError Traceback (most recent call last)
<ipython-input-55-6c66bfb457a5> in <module>
----> 1 check_output([f"grep -c 'def ' repos/clara.aaa_hotmail.com/*.py"], shell=True)
/usr/lib/python3.8/subprocess.py in check_output(timeout, *popenargs, **kwargs)
413 kwargs['input'] = empty
414
--> 415 return run(*popenargs, stdout=PIPE, timeout=timeout, check=True,
416 **kwargs).stdout
417
/usr/lib/python3.8/subprocess.py in run(input, capture_output, timeout, check, *popenargs, **kwargs)
514 retcode = process.poll()
515 if check and retcode:
--> 516 raise CalledProcessError(retcode, process.args,
517 output=stdout, stderr=stderr)
518 return CompletedProcess(process.args, retcode, stdout, stderr)
CalledProcessError: Command '["grep -c 'def ' repos/clara.aaa_hotmail.com/*.py"]' returned non-zero exit status 1.
In [56]:
Note: we are not dealing with pipe |
Cf. python subprocess.check_output doesn't return when cat | grep combination
CodePudding user response:
You can replace subprocess.check_output
with subprocess.run
and add the capture_output=True
>>> from subprocess import run
>>> run([f"grep -c 'def ' repos/saad.aaa_gmail.com/*.py"],
shell=True, capture_output=True)
b'3\n'
>>>