繁体   English   中英

运行bash命令后如何将shell输出输出到python中

[英]How to get shell output into python after it has run the bash command

有人知道如何将python bash代码的输出返回到python吗?

我在跑步:

import subprocess
output = subprocess.run("ls -l", shell=True, stdout=subprocess.PIPE, 
universal_newlines=True)
print(output.stdout)

如何将ls -l的输出返回到我的python代码中? 我可以将其转储到文件中,然后在python代码中调用该文件,但是有没有更简单的方法,我的代码可以直接读取输出而无需其他文件?

先感谢您。

您可以使用subprocess.getoutput

subprocess.getoutput("ls -l")

该函数返回一个字符串,然后您可以对其进行解析。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM