本文介绍了运行BASH内置命令在Python?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法运行在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')

和它们的许多变型。我想运行历史 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()

感谢大家对输入。

这篇关于运行BASH内置命令在Python?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-31 07:24