'use subprocess.run with check = True where the command is expected to return a non-zero exit code

When using subprocess.run it's recommended to use check = True (https://pycodequ.al/docs/pylint-messages/w1510-subprocess-run-check.html). However, there might be a subprocess I want to run where a non-zero exit code is expected, and I'm wondering whether there's an approach which will satisfy both pylint and my code.

For example:

subprocess.run('ls | grep sdfjosidjf', shell = True, check = True)

Will (most likely) raise:

CalledProcessError: Command 'ls | grep sdfjosidjf' returned non-zero exit status 1.

What if this is exactly what I'm expecting or even wanting though?

I could remove check = True and just disable pylint for this line, I'm wondering if there's a more general approach than check = True though, perhaps I'm expecting either 0 or 1 as exit codes.



Solution 1:[1]

Why not just set check=False?

subprocess.run('ls | grep sdfjosidjf', shell=True, check=False)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 richardec