[Tutor] Please correct my error handling
Mats Wichmann
mats at wichmann.us
Thu Jan 2 13:29:58 EST 2020
On 1/2/20 11:12 AM, Robert Alexander wrote:
> I am having an hard time to properly address some errors in external
> programs launched by my python code.
Popen fails directly if it can't open a process for the requested
command, so you have to check a different way for that case. Try this:
> # 3) repeat with an illegal ;) command
try:
process = subprocess.Popen(
['/bin/lsd', '-E'],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
)
except FileNotFoundError as e:
print("--Command not found--\n", e)
sys.exit(2)
# or you could just "raise":
stdout, stderr = process.communicate()
if not stderr:
print('--No errors--\n', stdout.decode())
else:
print('--Error found--\n', stderr.decode())
More information about the Tutor
mailing list