无法通过子处理

时间:2017-09-14 14:37:45

标签: python python-3.x error-handling subprocess try-except

有没有人知道你是否可以在python中捕获os错误。我正在编写一个映射网络驱动器的脚本。 如果最终用户键入不存在的路径的名称,则不会显示错误,这可能会成为问题。 我正在使用子进程模块,python版本3.6,以及pycharm IDE。 如果我映射到不存在的位置,我会收到以下错误"发生了系统错误53。找不到网络路径。"

最后,我尝试使用OSError,Exception和BaseException来捕获错误。我使用的每一个都不会返回任何错误消息。

以下是我正在运行的脚本示例。

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 个答案:

答案 0 :(得分:0)

  

Python»文档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