【发布时间】:2011-07-24 13:45:15
【问题描述】:
有没有办法从 Python 运行 BASH 内置命令?
我试过了:
subprocess.Popen(['bash','history'],shell=True, stdout=PIPE)
subprocess.Popen('history', shell=True, executable = "/bin/bash", stdout=subprocess.PIPE)
os.system('history')
及其许多变体。我想运行history 或fc -ln。
【问题讨论】:
-
第二个对我来说很合适。它出了什么问题?
-
运行一个 bash 进程并与之交互怎么样?对于长时间运行的命令或与 shell 交互(即在
sudo之后输入密码),它可能更简单且有用。我认为pexpect 可能适合这种需求。
标签: python bash command subprocess