你好,我正在尝试从其他脚本调用的外部python脚本取回结果(我知道是1)。什么时候回声$?从命令行我得到1,但是当我尝试调用echo $?从python获取python脚本的结果,我在脚本中得到0。这是我的代码:
os.system(pythonPath+"serialWait.py "+dev+" "+brate+" login") //this
os.system(pythonPath+"serialCommand.py "+dev+" "+brate+" reset") //this works
value = subprocess.call('echo $?', shell=True)
>//here is where my issue lies I am trying to call the exit code from the previous python script but only get the true value from terminal
参考方案
这是不可能的:$?是仅在单个shell期间存在的变量。每次调用os.system()时,都会创建一个新的外壳:旧外壳已经退出,因此该外壳内部的变量(例如$?)不再存在。
请使用subprocess模块:
p1 = subprocess.Popen(['serialWait.py', dev, brate, 'login'])
p1.wait()
if p1.returncode != 0:
print 'Proc