Python脚本在终端中执行命令

时间:2021-05-23 01:16:16

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

它将导致在终端中运行该命令的结果

7 个解决方案

#1


102  

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:

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

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

#2


12  

I prefer usage of subprocess module:

我更喜欢使用子进程模块:

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])

#3


5  

In fact any question on subprocess will be a good read

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

#4


1  

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']

#5


1  

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

os.popen()非常简单易用,但自Python 2.6以来它已被弃用。您应该使用子进程模块。

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

阅读此处:将os.popen(命令)读入字符串

#6


0  

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中。

#7


-6  

In the desktop GUI, (some of you may not be using it) you can leafpad a desktop shortcut and it has a command in it to open the deignated file. If you copy and paste it into your python interpreter, it should work. (Im not entirly sure that this will work, but why not give it a try!) Sorry for all the spelling errors... Im only 11. This info is NOT made up! that is just me being lazy while making up a name! remember, im only 11!

在桌面GUI中,(你们中的一些人可能没有使用它)你可以使用桌面快捷方式,并在其中有一个命令来打开指定的文件。如果将它复制并粘贴到python解释器中,它应该可以工作。 (我不确定这会起作用,但为什么不试一试!)抱歉所有的拼写错误...我只有11.这个信息没有组成!这只是我在制作名字时懒惰!记住,我只有11!

#1


102  

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:

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

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

#2


12  

I prefer usage of subprocess module:

我更喜欢使用子进程模块:

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])

#3


5  

In fact any question on subprocess will be a good read

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

#4


1  

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']

#5


1  

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

os.popen()非常简单易用,但自Python 2.6以来它已被弃用。您应该使用子进程模块。

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

阅读此处:将os.popen(命令)读入字符串

#6


0  

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中。

#7


-6  

In the desktop GUI, (some of you may not be using it) you can leafpad a desktop shortcut and it has a command in it to open the deignated file. If you copy and paste it into your python interpreter, it should work. (Im not entirly sure that this will work, but why not give it a try!) Sorry for all the spelling errors... Im only 11. This info is NOT made up! that is just me being lazy while making up a name! remember, im only 11!

在桌面GUI中,(你们中的一些人可能没有使用它)你可以使用桌面快捷方式,并在其中有一个命令来打开指定的文件。如果将它复制并粘贴到python解释器中,它应该可以工作。 (我不确定这会起作用,但为什么不试一试!)抱歉所有的拼写错误...我只有11.这个信息没有组成!这只是我在制作名字时懒惰!记住,我只有11!