Unable to hand errors with try/except, subprocess and bedtools


I am having an issue handling errors with try/except and subprocess.

   subprocess.run("bedtools interset ...", sheel=True)
except subprocess.CalledProcessError as e:

Bedtool is not able to identify one of the files and print it in the terminal

Error: Unable to open file

but this is not caught by the try/except. Python doesn’t run the except part and then keeps running. I know that it is bedtools the one that is printing the error message because if I delete the print(e.output) nothing changes.

What I am doing wrong?


What I am doing wrong?

subprocess.run docs claims that

If check is true, and the process exits with a non-zero exit code, a
CalledProcessError exception will be raised. Attributes of that
exception hold the arguments, the exit code, and stdout and stderr if
they were captured.

default value for check is False, so you need use check=True if you want behavior described above, that is you should be doing

subprocess.run("bedtools interset ...", shell=True, check=True)

Answered By – Daweo

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published