在某些情况下,当Callling GREP -C从subprocess.check_output中call trackback
我对subprocess.check_out
的行为非常奇怪:
我想获得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]:
在此方面,我可以很好地得到
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'
:
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]:
注意:我们是不处理管道|
参见。 python subprocess.check_output GREP组合
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以用
subprocess.check_output
用subprocess.run
,然后添加captare_output = true
You can replace
subprocess.check_output
withsubprocess.run
and add thecapture_output=True