【问题标题】:Python: I want to execute shell commands using python scriptPython:我想使用 python 脚本执行 shell 命令
【发布时间】:2017-03-29 15:10:34
【问题描述】:

我想在我的研究工作中使用 python 执行 shell 命令“objdump” 我已经使用subprocess.call("command") 执行linux命令,但它不起作用。

我尝试过的示例代码是

import subprocess
subprocess.call("date")

执行后

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Python27\lib\subprocess.py", line 168, in call
    return Popen(*popenargs, **kwargs).wait()
  File "C:\Python27\lib\subprocess.py", line 390, in __init__
    errread, errwrite)
  File "C:\Python27\lib\subprocess.py", line 640, in _execute_child
    startupinfo)
WindowsError: [Error 2] The system cannot find the file specified

【问题讨论】:

  • 我很确定回溯不是来自 Ubuntu 系统!

标签: python shell ubuntu


【解决方案1】:

你必须这样做:

subprocess.call('date', shell=True)

实际上,Shell 确实允许您访问 $PATH 和您的 shell 中的全局变量和程序。

【讨论】:

    【解决方案2】:

    你试过了吗?

    import subprocess
    subprocess.call("date", shell = True)
    

    【讨论】:

    • 但是当我尝试使用 IDLE 时,它返回 0。