5

In Python, I run an exe made using FORTRAN. I use the subprocess module. that exe accesses and writes to several files. If I make those files readonly, I see the following trace in my Python console.

I tried by using try, except statements. But I could not capture the error. I also tried using p.stdout.readline(). But was unsuccessful.

Is there a systematic way to capture this sort of errors.

Code:

import subprocess
p = subprocess.Popen('C:\\TGSSR\\test.exe' , shell=True, stdout=subprocess.PIPE)

Traceback:

forrtl: severe (9): permission to access file denied, unit 6, file C:\test\mar22_SSOUT\RawReadLog.dat

Image              PC        Routine            Line        Source             
test.exe           0116DC40  Unknown               Unknown  Unknown
test.exe           0113D42F  Unknown               Unknown  Unknown
test.exe           0112AE97  Unknown               Unknown  Unknown
test.exe           0112A1DA  Unknown               Unknown  Unknown
test.exe           0110D746  Unknown               Unknown  Unknown
test.exe           0108B9AC  Unknown               Unknown  Unknown
test.exe           01173FE3  Unknown               Unknown  Unknown
test.exe           011588F5  Unknown               Unknown  Unknown
kernel32.dll       76D33677  Unknown               Unknown  Unknown
ntdll.dll          77A39F42  Unknown               Unknown  Unknown
ntdll.dll          77A39F15  Unknown               Unknown  Unknown
P̲̳x͓L̳
  • 3,615
  • 3
  • 29
  • 37
py_works
  • 190
  • 1
  • 1
  • 14
  • 1
    unrelated: to avoid escaping backslashes, you could use raw-string literals: `r'C:\TGSSR\test.exe'`. Don't use `shell=True` on Windows unless you use the shell functionality e.g., to call a builtin shell command such as `dir`. Don't use `stdout=PIPE` unless you read from `p.stdout` later – jfs Mar 23 '14 at 04:27
  • Sometimes errors are written outside stdout/stderr (directly to the terminal). See [Capture “Segmentation fault” message for a crashed subprocess: no out and err after a call to communicate()](http://stackoverflow.com/q/22250893/4279) – jfs Mar 23 '14 at 13:07

4 Answers4

7

Run the process:

p = subprocess.Popen(['C:\\TGSSR\\test.exe'], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
# shell = True is not needed

To capture the error message:

stdout, stderr = p.communicate()
# stdout = normal output
# stderr = error output

Check the process return code:

if p.returncode != 0:
    # handle error
kirbyfan64sos
  • 10,377
  • 6
  • 54
  • 75
  • 1
    While maybe not necessary on windows, if you're going to not use a shell, you should pass the executable as a list: `['C:\\TGSSR\\test.ext']`. Also, the returncode may not be reliable as (to my knowledge), there still isn't a good standardized way to set a process's return code in Fortran. – mgilson Mar 23 '14 at 02:20
  • in this case p is null in both states (successful or unsuccessful). So I'm unable to rely on that! – py_works Mar 23 '14 at 03:07
  • @mgilson: `Popen("program")` works. It is equivalent to `Popen(["program"])` on POSIX. On Windows, a string argument always works. – jfs Mar 23 '14 at 04:34
  • @user3161836: `p` is not null; you might mean `p.returncode is None`. The latter means that the process is not complete yet. You should check `p.returncode` *after* `p.communicate()` returns (it waits for the process to finish) – jfs Mar 23 '14 at 04:36
  • @mgilson: I actually forgot about that. Thanks for the pointing that out. – kirbyfan64sos Mar 24 '14 at 20:27
  • @J.F.Sebastian: you know `p.returncode != 0` is the same thing as `p.returncode`, right? – kirbyfan64sos Mar 24 '14 at 20:29
  • @kirbyfan64sos: `p.returncode` may be `None` as my comment mentions. Moreover I mention it in [the revision description](http://stackoverflow.com/revisions/22586242/2) and it seems (due to a bug) OP got it. `bool(p.returncode)` is `p.returncode is not None and p.returncode != 0` in this case – jfs Mar 25 '14 at 00:49
3

Python 3.5 introduced the subprocess.run() method.

Here is the @kirbyfan64sos answer updated for Python >=3.5:

Run the process:

p = subprocess.run(['C:\\TGSSR\\test.exe'], stdout=subprocess.PIPE, stderr=subprocess.PIPE)

In >=3.5, you can access the returncode, stdout, and stderr from the executed process.

To capture the error message:

stdout = p.stdout # stdout = normal output
stderr = p.stderr # stderr = error output

Check the process return code:

if p.returncode != 0:
# handle error
1

If don't need all of Popen's functionality but just need to fetch stdout, you could also go for:

try:
    output = subprocess.check_output('C:\\TGSSR\\test.exe')
except subprocess.CalledProcessError as e:
    print("Oops... returncode: " + e.returncode + ", output:\n" + e.output)
else:
    print("Everything ok:\n" + output)

EDIT: As mgilson pointed out in the other answer, this requires a non-zero returncode on failures. If that isn't the case, you could try something along the lines of:

output = subprocess.check_output('C:\\TGSSR\\test.exe')
if "permission to access file denied" in output:
    print("Failed")

with some string that will only be on stdout in case of an error

Wisperwind
  • 963
  • 7
  • 16
  • it should be `subprocess.CalledProcessError` in this case – m.wasowski Mar 23 '14 at 02:25
  • This works for me.if I just need to run the exe, I think I can go for this option. Am I going to mis any major functionality if I go this way? – py_works Mar 23 '14 at 03:12
  • 1
    That was probably badly worded, check_output is a convenience function based on Popen which doesn't require handling stdout/stderr yourself (in your first post, you would actually need at least a p.communicate() in order to prevent the stdout pipe buffer from completely filling if you FORTRAN tool outputs a lot which would effectively stall execution). You can however easily switch to Popen. Popen gives more fine-grained control of process creation: [link](http://docs.python.org/3/library/subprocess.html#popen-constructor) – Wisperwind Mar 23 '14 at 03:25
0

In the majority of cases, you need to use subprocess.run. And to capture the error, you have to use the parameter "capture_output=True, check=True"

check=True is really necessary.

try:
    subprocess.run(args, cwd, capture_output=True, check=True)
except subprocess.CalledProcessError as e:
    print("Error while executing YoloV5:")
    for k, v in e.__dict__.items():
        print(k)
        if isinstance(v, bytes):
            print(v.decode("utf-8"))
        else:
            print(v)
    raise Exception("Error in subcommand")