首先,为什么要用python处理I/O?只需让子进程的stdout和stderr与python的stdout和stderr放在同一个地方。第二,您可以直接从python检索资源,而不是实际使用time命令。试试这样的方法:#! /usr/bin/python
import os
import resource
import sys
import time
cmd = ' '.join(sys.argv[1:])
stime = time.time()
os.system(cmd) # fire off the command
etime = time.time()
# now get the resource utilization
r = resource.getrusage(resource.RUSAGE_CHILDREN)
user_time = r.ru_utime
sys_time = r.ru_stime
# resource doesn't know "clock" time, so we'll just figure that ourselves
real_time = etime - stime
print "real\t" + str(real_time)
print "user\t" + str(user_time)
print "sys\t" + str(sys_time)
它以秒为单位打印时间。如果您真的希望它们看起来像time命令,可以相应地格式化它们。在
回答你问题的第二部分,实际上有不同的“时间”命令。当您将其作为python的子级运行时,将得到/usr/bin/time的输出。当您手动运行它时,您将获得shell的内置版本time。尝试在shell提示符处键入“type-a time”。另外,试着像这样运行您的测试程序:“/usr/bin/time/测试.py“你应该看看第二种形式。在