Can not catch os error with subprocessing

451 Views Asked by At

Does anyone know if you can catch os errors in python. I am working on a script that is mapping network drives. If the end user types the name of a path that does not exist no error shows up and that could become an issues. I am using the subprocess module, with python version 3.6, along with the pycharm IDE. If I map to a location that does not exist I get the following error "System error 53 has occurred. The network path was not found."

Lastly I have tried catching the error using OSError, Exception, and BaseException. Each one that I have used does not return any error message.

Here is an example of the script that I am running.

def StoreInfo():
receiving = input("Enter receiving: ")
try:
    subprocess.call(r'net use y: \\' + receiving + '\\act\\rto\\transent', shell=True) #timeout=30
except OSError:
    #print("An unexpected error:" , sys.exc_info()[0])
    print(sending+"'s path does not exit!")

StoreInfo()

1

There are 1 best solutions below

0
On

Python » Documentation Subprocess

try: 
    retcode = call("mycmd" + " myarg", shell=True) 
    if retcode < 0: 
        print >>sys.stderr, "Child was terminated by signal", -retcode 
    else: 
        print >>sys.stderr, "Child returned", retcode 
except OSError as e: 
    print >>sys.stderr, "Execution failed:", e