-2

I am using a shell script to run the commands.

When running the command using this

os.popen(command).readline()

I want to get the exit code of that command.

Please let me know how we get the exit code in this case.

Gino Mempin
  • 25,369
  • 29
  • 96
  • 135
Shanthi K
  • 5
  • 6

1 Answers1

3

When you call close() on the file object returned by os.popen(), it returns the termination status if it's not 0. You use os.waitstatus_to_exitcode() to extract the exit code from this.

f = os.popen(command)
line = f.readline()
status = f.close()
if status:
    print(os.waitstatus_to_exitcode(status))
Barmar
  • 741,623
  • 53
  • 500
  • 612