如何捕获正在从另一个 python 脚本执行的 python 脚本的打印?
How to capture the prints of a python script being executed from another python script?
我在同一文件夹中有 2 个脚本 script1.py
和 script2.py
,script1.py 使用 Popen 调用 script2.py(详情请参见下面的代码),问题是来自 script2.py 的打印未在 script1.py、print output
中捕获并且 print error
未在下面的代码中打印任何内容?我在这里错过了什么?我如何从 script2.py?
捕获指纹
script1.py
import subprocess
from subprocess import Popen, PIPE, STDOUT
def func1 ():
cmd = "python script2.py"
proc = Popen(cmd.split(' '), stdout=PIPE, stderr=PIPE)
(output, error) = proc.communicate()
print output
print error
func1()
print "Done.."
script2.py
import sys
print "ERROR:port not detected"
sys.exit(29)
输出:-
C:\Dropbox>python script1.py
ERROR:port not detected
Done..
根据评论编辑答案
看起来在您对原始问题进行编辑后,您的代码运行正常。
我只是把 output=
放在 print 语句前面来检查一下。
import subprocess
from subprocess import Popen, PIPE, STDOUT
def func1 ():
cmd = "python script2.py"
proc = Popen(cmd.split(' '), stdout=PIPE, stderr=PIPE)
(output, error) = proc.communicate()
print "output=",output
print error
func1()
print "Done.."
** 输出:**
Python 2.7.9 (default, Dec 10 2014, 12:24:55) [MSC v.1500 32 bit (Intel)] on win32
Type "copyright", "credits" or "license()" for more information.
>>> ================================ RESTART ================================
>>>
output= ERROR:port not detected
Done..
>>>
您的脚本实际上正在按预期工作。您可能希望将回溯打印到子进程的 stderr,但这不是 sys.exit()
的工作方式。
script1.py
import subprocess
from subprocess import Popen, PIPE, STDOUT
def func1 ():
cmd = "python script2.py"
proc = Popen(cmd.split(' '), stdout=PIPE, stderr=PIPE)
(output, error) = proc.communicate()
print output[::-1] #prints reversed message proving print is called from script1 not script2
print error #prints nothing because there is no error text (no error was raised only system exit)
print 'return status: '+str(proc.returncode) #this is what sys.exit() modifies
func1()
print "Done.. YAY" #done prints after call
script2.py
import sys
print "ERROR:port not detected"
sys.exit(29) #does not print traceback (or anything) only sets return code
print "Done.." #never prints because python interpreter is terminated
当 script1 将 script2 作为子进程调用时,script2 将其第一条语句打印到标准输出管道,然后以 return 代码 29 退出。 return 代码为 0 的系统退出是被视为成功 return,因此除非您专门调用引发错误的内容,否则不会向 stderr 管道打印任何内容。然而,return 代码可以根据您的 proc
.
的属性 returncode
确定
运行 >python script1.py
产量:
detceted ton trop:RORRE
return status: 29
Done.. YAY
我在同一文件夹中有 2 个脚本 script1.py
和 script2.py
,script1.py 使用 Popen 调用 script2.py(详情请参见下面的代码),问题是来自 script2.py 的打印未在 script1.py、print output
中捕获并且 print error
未在下面的代码中打印任何内容?我在这里错过了什么?我如何从 script2.py?
script1.py
import subprocess
from subprocess import Popen, PIPE, STDOUT
def func1 ():
cmd = "python script2.py"
proc = Popen(cmd.split(' '), stdout=PIPE, stderr=PIPE)
(output, error) = proc.communicate()
print output
print error
func1()
print "Done.."
script2.py
import sys
print "ERROR:port not detected"
sys.exit(29)
输出:-
C:\Dropbox>python script1.py
ERROR:port not detected
Done..
根据评论编辑答案
看起来在您对原始问题进行编辑后,您的代码运行正常。
我只是把 output=
放在 print 语句前面来检查一下。
import subprocess
from subprocess import Popen, PIPE, STDOUT
def func1 ():
cmd = "python script2.py"
proc = Popen(cmd.split(' '), stdout=PIPE, stderr=PIPE)
(output, error) = proc.communicate()
print "output=",output
print error
func1()
print "Done.."
** 输出:**
Python 2.7.9 (default, Dec 10 2014, 12:24:55) [MSC v.1500 32 bit (Intel)] on win32
Type "copyright", "credits" or "license()" for more information.
>>> ================================ RESTART ================================
>>>
output= ERROR:port not detected
Done..
>>>
您的脚本实际上正在按预期工作。您可能希望将回溯打印到子进程的 stderr,但这不是 sys.exit()
的工作方式。
script1.py
import subprocess
from subprocess import Popen, PIPE, STDOUT
def func1 ():
cmd = "python script2.py"
proc = Popen(cmd.split(' '), stdout=PIPE, stderr=PIPE)
(output, error) = proc.communicate()
print output[::-1] #prints reversed message proving print is called from script1 not script2
print error #prints nothing because there is no error text (no error was raised only system exit)
print 'return status: '+str(proc.returncode) #this is what sys.exit() modifies
func1()
print "Done.. YAY" #done prints after call
script2.py
import sys
print "ERROR:port not detected"
sys.exit(29) #does not print traceback (or anything) only sets return code
print "Done.." #never prints because python interpreter is terminated
当 script1 将 script2 作为子进程调用时,script2 将其第一条语句打印到标准输出管道,然后以 return 代码 29 退出。 return 代码为 0 的系统退出是被视为成功 return,因此除非您专门调用引发错误的内容,否则不会向 stderr 管道打印任何内容。然而,return 代码可以根据您的 proc
.
returncode
确定
运行 >python script1.py
产量:
detceted ton trop:RORRE return status: 29 Done.. YAY