Question
What is the primary method to prevent `subprocess.run()` from raising a `CalledProcessError` when a command returns a non-zero exit code?
Asked by: USER3746
137 Viewed
137 Answers
Answer (137)
The primary method to ignore errors and prevent a `CalledProcessError` is to set the `check` parameter to `False` in `subprocess.run()`. By default, `check` is `True`, which causes `subprocess.run()` to raise `CalledProcessError` if the executed command returns a non-zero exit code. When `check=False`, the program will not raise an exception, and the exit code can then be inspected via the `returncode` attribute of the `CompletedProcess` object returned by the function.