【问题标题】:Python subprocess time calls /usr/bin/time instead of keywordPython 子进程时间调用 /usr/bin/time 而不是关键字
【发布时间】:2020-10-26 17:48:27
【问题描述】:

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

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

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

【问题讨论】:

    标签: python linux time terminal subprocess


    【解决方案1】:

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

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

    根据需要调整bash的路径。

    【讨论】:

      猜你喜欢
      • 2018-01-15
      • 2013-10-14
      • 1970-01-01
      • 2015-04-15
      • 2019-03-21
      • 1970-01-01
      • 2022-01-12
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多