Python脚本在终端中执行命令 [英] Python Script execute commands in Terminal
本文介绍了Python脚本在终端中执行命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我不久前在某处读到过这个,但似乎找不到.我试图找到一个命令,该命令将在终端中执行命令,然后输出结果.
I read this somewhere a while ago but cant seem to find it. I am trying to find a command that will execute commands in the terminal and then output the result.
例如:脚本将是:
command 'ls -l'
它会输出在终端中运行该命令的结果
It will out the result of running that command in the terminal
推荐答案
有几种方法可以做到这一点:
There are several ways to do this:
一个简单的方法是使用 os 模块:
A simple way is using the os module:
import os
os.system("ls -l")
使用 subprocess 模块可以实现更复杂的事情:例如:
More complex things can be achieved with the subprocess module: for example:
import subprocess
test = subprocess.Popen(["ping","-W","2","-c", "1", "192.168.1.70"], stdout=subprocess.PIPE)
output = test.communicate()[0]
这篇关于Python脚本在终端中执行命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文