在某些情况下,当Callling GREP -C从subprocess.check_output中call tr​​ackback

发布于 2025-02-04 14:07:58 字数 1932 浏览 5 评论 0原文

我对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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

杀お生予夺 2025-02-11 14:07:58

您可以用subprocess.check_outputsubprocess.run,然后添加captare_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'
>>>

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'
>>>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文