Python 子进程时间调用 /usr/bin/time 而不是关键字

Python subprocess time calls /usr/bin/time instead of keyword

我正在尝试获取进程的时间,当我在 shell 中使用关键字 time 时,我得到了更好的输出:

real    0m0,430s
user    0m0,147s
sys     0m0,076s

而不是 /usr/bin/time 会给出不同的输出。当我尝试通过 python 的子进程库 subprocess.call('time command args',shell=True) 运行 它时,它给了我 /usr/bin/time 而不是关键字。我怎样才能使用关键字功能而不是当前的功能?

shell=True 导致 subprocess 使用 /bin/sh,而不是 bash。您还需要 executable 参数

subprocess.call('time command args', shell=True, executable='/bin/bash')

根据需要将路径调整为 bash