Python脚本在终端中执行命令

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3730964/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 12:28:00  来源:igfitidea点击:

Python Script execute commands in Terminal

pythonterminal

提问by Ali

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.

不久前我在某处读过这个,但似乎找不到它。我试图找到一个将在终端中执行命令然后输出结果的命令。

For example: the script will be:

例如:脚本将是:

command 'ls -l'

It will out the result of running that command in the terminal

它将输出在终端中运行该命令的结果

采纳答案by Uku Loskit

There are several ways to do this:

做这件事有很多种方法:

A simple way is using the os module:

一个简单的方法是使用 os 模块:

import os
os.system("ls -l")

More complex things can be achieved with the subprocess module: for example:

使用 subprocess 模块可以实现更复杂的事情:例如:

import subprocess
test = subprocess.Popen(["ping","-W","2","-c", "1", "192.168.1.70"], stdout=subprocess.PIPE)
output = test.communicate()[0]

回答by pyfunc

In fact any question on subprocess will be a good read

事实上,任何关于子流程的问题都将是一个很好的阅读

回答by minocha

You should also look into commands.getstatusoutput

您还应该查看 commands.getstatusoutput

This returns a tuple of length 2.. The first is the return integer ( 0 - when the commands is successful ) second is the whole output as will be shown in the terminal.

这将返回一个长度为 2 的元组。第一个是返回整数(0 - 当命令成功时),第二个是整个输出,将在终端中显示。

For ls

对于 ls

    import commands
    s=commands.getstatusoutput('ls')
    print s
    >> (0, 'file_1\nfile_2\nfile_3')
    s[1].split("\n")
    >> ['file_1', 'file_2', 'file_3']

回答by Paolo Rovelli

The os.popen()is pretty simply to use, but it has been deprecated since Python 2.6. You should use the subprocessmodule instead.

os.popen()是非常简单的使用,但因为Python 2.6已经过时。您应该改用subprocess模块。

Read here: reading a os.popen(command) into a string

在这里阅读:将 os.popen(command) 读入字符串

回答by Kevin Pandya

I prefer usage of subprocess module:

我更喜欢使用 subprocess 模块:

from subprocess import call
call(["ls", "-l"])

Reason is that if you want to pass some variable in the script this gives very easy way for example take the following part of the code

原因是,如果您想在脚本中传递一些变量,这提供了非常简单的方法,例如采用以下部分代码

abc = a.c
call(["vim", abc])

回答by Mr_pzling_Pie

import os
os.system("echo 'hello world'")

This should work. I do not know how to print the output into the python Shell.

这应该有效。我不知道如何将输出打印到 python Shell 中。

回答by Kobe Keirouz

You could import the 'os' module and use it like this :

您可以导入 'os' 模块并像这样使用它:

import os
os.system('#DesiredAction')

回答by Zephro

Jupyter

木星

In a jupyter notebook you can use the magic function !

在 jupyter notebook 中,您可以使用魔法功能 !

!echo "execute a command"
files = !ls -a /data/dir/ #get the output into a variable

ipython

蟒蛇

To execute this as a .pyscript you would need to use ipython

要将其作为.py脚本执行,您需要使用ipython

files = get_ipython().getoutput('ls -a /data/dir/')

execute script

执行脚本

$ ipython my_script.py