有没有办法从 Python 运行 BASH 内置命令?
Is there a way to run the BASH built-in commands from Python?
我试过了:
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
.
and many variations thereof. I would like to run history
or fc -ln
.
我终于找到了一个可行的解决方案.
I finally found a solution that works.
from subprocess import Popen, PIPE, STDOUT
shell_command = 'bash -i -c "history -r; history"'
event = Popen(shell_command, shell=True, stdin=PIPE, stdout=PIPE,
stderr=STDOUT)
output = event.communicate()
感谢大家的意见.
这篇关于在 Python 中运行 BASH 内置命令?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!