问题:终端中的Python脚本执行命令

我在某个地方读过这篇文章,但似乎找不到。我试图找到一个将在终端中执行命令然后输出结果的命令。

例如:脚本将是:

command 'ls -l'

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

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


回答 0

做这件事有很多种方法:

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

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

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

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

There are several ways to do this:

A simple way is using the os module:

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]

回答 1

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

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

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

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

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

回答 2

实际上,关于子流程的任何问题都将是一本好书


回答 3

您还应该查看commands.getstatusoutput

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

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

You should also look into 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.

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

回答 4

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

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

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

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


回答 5

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

在此处阅读:将os.popen(command)读入字符串

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

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


回答 6

朱皮特

在Jupyter笔记本电脑中,您可以使用魔术功能 !

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

ipython的

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

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

执行脚本

$ ipython my_script.py

Jupyter

In a jupyter notebook you can use the magic function !

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

ipython

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

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

execute script

$ ipython my_script.py

回答 7

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

import os
os.system('#DesiredAction')

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

import os
os.system('#DesiredAction')

回答 8

对于python3使用子进程

import subprocess
s = subprocess.getstatusoutput(f'ps -ef | grep python3')
print(s)

for python3 use subprocess

import subprocess
s = subprocess.getstatusoutput(f'ps -ef | grep python3')
print(s)

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。