Command to get the exit code for "os.popen(command)" when we execute in shell

I am using shell script to run the commands. After running the command using this (os.popen(command).readline()), i want to get the exit code of the same.

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


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))