将 subprocess.run 与 check = True 一起使用,其中命令预计返回非零退出代码

bax*_*axx 5 python subprocess pylint

使用时subprocess.run建议使用check = Truehttps://pycodequ.al/docs/pylint-messages/w1510-subprocess-run-check.html)。但是,我可能想在需要非零退出代码的地方运行一个子进程,我想知道是否有一种方法可以同时满足 pylint 和我的代码。

例如:

subprocess.run('ls | grep sdfjosidjf', shell = True, check = True)
Run Code Online (Sandbox Code Playgroud)

将(最有可能)提高:

CalledProcessError: Command 'ls | grep sdfjosidjf' returned non-zero exit status 1.
Run Code Online (Sandbox Code Playgroud)

如果这正是我所期待甚至想要的怎么办?

我可以删除check = True并禁用这一行的 pylint,我想知道是否有比这更通用的方法check = True,也许我期待01作为退出代码。

小智 6

为什么不直接设置check=False呢?

subprocess.run('ls | grep sdfjosidjf', shell=True, check=False)
Run Code Online (Sandbox Code Playgroud)