问题:从Python调用外部命令
您如何在Python脚本中调用外部命令(就像我在Unix Shell或Windows命令提示符下键入的一样)?
How do you call an external command (as if I’d typed it at the Unix shell or Windows command prompt) from within a Python script?
回答 0
查看标准库中的子流程模块:
import subprocess
subprocess.run(["ls", "-l"])
的优势subprocess
主场迎战system
的是,它是更灵活(你可以得到的stdout
,stderr
,“真正”的状态代码,更好的错误处理,等等)。
在官方文件建议subprocess
在替代模块os.system()
:
该subprocess
模块提供了更强大的功能来生成新流程并检索其结果。使用该模块优于使用此功能[ os.system()
]。
在与子模块更换旧的功能中部分subprocess
文件可能有一些有益的食谱。
对于3.5之前的Python版本,请使用call
:
import subprocess
subprocess.call(["ls", "-l"])
Look at the subprocess module in the standard library:
import subprocess
subprocess.run(["ls", "-l"])
The advantage of subprocess
vs. system
is that it is more flexible (you can get the stdout
, stderr
, the “real” status code, better error handling, etc…).
The official documentation recommends the subprocess
module over the alternative os.system()
:
The subprocess
module provides more powerful facilities for spawning new processes and retrieving their results; using that module is preferable to using this function [os.system()
].
The Replacing Older Functions with the subprocess Module section in the subprocess
documentation may have some helpful recipes.
For versions of Python before 3.5, use call
:
import subprocess
subprocess.call(["ls", "-l"])
回答 1
下面总结了调用外部程序的方法以及每种方法的优缺点:
os.system("some_command with args")
将命令和参数传递到系统的外壳程序。很好,因为您实际上可以以这种方式一次运行多个命令,并设置管道和输入/输出重定向。例如:
os.system("some_command < input_file | another_command > output_file")
但是,尽管这样做很方便,但您必须手动处理转义字符(例如空格等)的外壳字符。另一方面,这也使您可以运行仅是外壳程序命令而非实际上是外部程序的命令。请参阅文档。
stream = os.popen("some_command with args")
os.system
除了会为您提供类似于文件的对象之外,您可以使用该对象来访问该过程的标准输入/输出,它的作用与之相同。Popen还有其他3种变体,它们对I / O的处理略有不同。如果您将所有内容都作为字符串传递,那么您的命令将传递到外壳程序;如果将它们作为列表传递,则无需担心转义任何内容。请参阅文档。
模块的Popen
类subprocess
。它旨在替代它,os.popen
但缺点是由于太全面而使它稍微复杂一些。例如,您会说:
print subprocess.Popen("echo Hello World", shell=True, stdout=subprocess.PIPE).stdout.read()
代替:
print os.popen("echo Hello World").read()
但是将所有选项都放在一个统一的类中而不是4个不同的popen函数是一件好事。请参阅文档。
call
来自subprocess
模块的功能。基本上就像Popen
类一样,并接受所有相同的参数,但是它只是等待命令完成并提供返回代码。例如:
return_code = subprocess.call("echo Hello World", shell=True)
请参阅文档。
如果您使用的是Python 3.5或更高版本,则可以使用新subprocess.run
函数,该函数与上面的代码非常相似,但是更加灵活,并CompletedProcess
在命令完成执行后返回一个对象。
os模块还具有您在C程序中拥有的所有fork / exec / spawn函数,但是我不建议直接使用它们。
该subprocess
模块可能是您所使用的模块。
最后,请注意,对于所有方法,在这些方法中,您将要由外壳执行的最终命令作为字符串传递给您,并且您有责任对其进行转义。如果您传递的字符串的任何部分不能被完全信任,则将带来严重的安全隐患。例如,如果用户正在输入字符串的某些/任何部分。如果不确定,请仅将这些方法与常量一起使用。为了给您暗示的含义,请考虑以下代码:
print subprocess.Popen("echo %s " % user_input, stdout=PIPE).stdout.read()
并想象用户输入了“我的妈妈不爱我&& rm -rf /”这可能会擦除整个文件系统的信息。
Here’s a summary of the ways to call external programs and the advantages and disadvantages of each:
os.system("some_command with args")
passes the command and arguments to your system’s shell. This is nice because you can actually run multiple commands at once in this manner and set up pipes and input/output redirection. For example:
os.system("some_command < input_file | another_command > output_file")
However, while this is convenient, you have to manually handle the escaping of shell characters such as spaces, etc. On the other hand, this also lets you run commands which are simply shell commands and not actually external programs. See the documentation.
stream = os.popen("some_command with args")
will do the same thing as os.system
except that it gives you a file-like object that you can use to access standard input/output for that process. There are 3 other variants of popen that all handle the i/o slightly differently. If you pass everything as a string, then your command is passed to the shell; if you pass them as a list then you don’t need to worry about escaping anything. See the documentation.
The Popen
class of the subprocess
module. This is intended as a replacement for os.popen
but has the downside of being slightly more complicated by virtue of being so comprehensive. For example, you’d say:
print subprocess.Popen("echo Hello World", shell=True, stdout=subprocess.PIPE).stdout.read()
instead of:
print os.popen("echo Hello World").read()
but it is nice to have all of the options there in one unified class instead of 4 different popen functions. See the documentation.
The call
function from the subprocess
module. This is basically just like the Popen
class and takes all of the same arguments, but it simply waits until the command completes and gives you the return code. For example:
return_code = subprocess.call("echo Hello World", shell=True)
See the documentation.
If you’re on Python 3.5 or later, you can use the new subprocess.run
function, which is a lot like the above but even more flexible and returns a CompletedProcess
object when the command finishes executing.
The os module also has all of the fork/exec/spawn functions that you’d have in a C program, but I don’t recommend using them directly.
The subprocess
module should probably be what you use.
Finally please be aware that for all methods where you pass the final command to be executed by the shell as a string and you are responsible for escaping it. There are serious security implications if any part of the string that you pass can not be fully trusted. For example, if a user is entering some/any part of the string. If you are unsure, only use these methods with constants. To give you a hint of the implications consider this code:
print subprocess.Popen("echo %s " % user_input, stdout=PIPE).stdout.read()
and imagine that the user enters something “my mama didnt love me && rm -rf /” which could erase the whole filesystem.
回答 2
典型的实现:
import subprocess
p = subprocess.Popen('ls', shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
for line in p.stdout.readlines():
print line,
retval = p.wait()
您可以随意使用stdout
管道中的数据进行所需的操作。实际上,您可以简单地省略这些参数(stdout=
和stderr=
),其行为类似于os.system()
。
Typical implementation:
import subprocess
p = subprocess.Popen('ls', shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
for line in p.stdout.readlines():
print line,
retval = p.wait()
You are free to do what you want with the stdout
data in the pipe. In fact, you can simply omit those parameters (stdout=
and stderr=
) and it’ll behave like os.system()
.
回答 3
关于从调用者中分离子进程的一些提示(在后台启动子进程)。
假设您要从CGI脚本开始一个长任务。也就是说,子进程的生存期应比CGI脚本执行进程的生存期长。
子流程模块文档中的经典示例是:
import subprocess
import sys
# Some code here
pid = subprocess.Popen([sys.executable, "longtask.py"]) # Call subprocess
# Some more code here
这里的想法是,您不想在long任务.py完成之前在“调用子进程”行中等待。但是尚不清楚示例中“这里有更多代码”行之后会发生什么。
我的目标平台是FreeBSD,但是开发是在Windows上进行的,因此我首先在Windows上遇到了问题。
在Windows(Windows XP)上,直到longtask.py完成工作后,父进程才会完成。这不是CGI脚本中想要的。这个问题不是特定于Python的。在PHP社区中,问题是相同的。
解决方案是将DETACHED_PROCESS 进程创建标志传递给Windows API中的基础CreateProcess函数。如果碰巧安装了pywin32,则可以从win32process模块中导入该标志,否则您应该自己定义它:
DETACHED_PROCESS = 0x00000008
pid = subprocess.Popen([sys.executable, "longtask.py"],
creationflags=DETACHED_PROCESS).pid
/ * UPD 2015.10.27 @eryksun在下面的注释中指出,语义正确的标志是CREATE_NEW_CONSOLE(0x00000010)* /
在FreeBSD上,我们还有另一个问题:父进程完成后,它也会完成子进程。那也不是您在CGI脚本中想要的。一些实验表明,问题似乎出在共享sys.stdout。可行的解决方案如下:
pid = subprocess.Popen([sys.executable, "longtask.py"], stdout=subprocess.PIPE, stderr=subprocess.PIPE, stdin=subprocess.PIPE)
我没有检查其他平台上的代码,也不知道FreeBSD上该行为的原因。如果有人知道,请分享您的想法。谷歌搜索在Python中启动后台进程尚未阐明。
Some hints on detaching the child process from the calling one (starting the child process in background).
Suppose you want to start a long task from a CGI script. That is, the child process should live longer than the CGI script execution process.
The classical example from the subprocess module documentation is:
import subprocess
import sys
# Some code here
pid = subprocess.Popen([sys.executable, "longtask.py"]) # Call subprocess
# Some more code here
The idea here is that you do not want to wait in the line ‘call subprocess’ until the longtask.py is finished. But it is not clear what happens after the line ‘some more code here’ from the example.
My target platform was FreeBSD, but the development was on Windows, so I faced the problem on Windows first.
On Windows (Windows XP), the parent process will not finish until the longtask.py has finished its work. It is not what you want in a CGI script. The problem is not specific to Python; in the PHP community the problems are the same.
The solution is to pass DETACHED_PROCESS Process Creation Flag to the underlying CreateProcess function in Windows API.
If you happen to have installed pywin32, you can import the flag from the win32process module, otherwise you should define it yourself:
DETACHED_PROCESS = 0x00000008
pid = subprocess.Popen([sys.executable, "longtask.py"],
creationflags=DETACHED_PROCESS).pid
/* UPD 2015.10.27 @eryksun in a comment below notes, that the semantically correct flag is CREATE_NEW_CONSOLE (0x00000010) */
On FreeBSD we have another problem: when the parent process is finished, it finishes the child processes as well. And that is not what you want in a CGI script either. Some experiments showed that the problem seemed to be in sharing sys.stdout. And the working solution was the following:
pid = subprocess.Popen([sys.executable, "longtask.py"], stdout=subprocess.PIPE, stderr=subprocess.PIPE, stdin=subprocess.PIPE)
I have not checked the code on other platforms and do not know the reasons of the behaviour on FreeBSD. If anyone knows, please share your ideas. Googling on starting background processes in Python does not shed any light yet.
回答 4
import os
os.system("your command")
请注意,这很危险,因为未清除命令。我留给你去谷歌搜索有关“操作系统”和“系统”模块的相关文档。有很多函数(exec *和spawn *)将执行类似的操作。
import os
os.system("your command")
Note that this is dangerous, since the command isn’t cleaned. I leave it up to you to google for the relevant documentation on the ‘os’ and ‘sys’ modules. There are a bunch of functions (exec* and spawn*) that will do similar things.
回答 5
我建议你使用子模块,而不是使用os.system因为它没有外壳逃避你,因此更加安全。
subprocess.call(['ping', 'localhost'])
I’d recommend using the subprocess module instead of os.system because it does shell escaping for you and is therefore much safer.
subprocess.call(['ping', 'localhost'])
回答 6
import os
cmd = 'ls -al'
os.system(cmd)
如果要返回命令的结果,可以使用os.popen
。但是,自2.6版以来,不推荐使用此方法,而支持subprocess模块,其他答案已经很好地涵盖了这一点。
import os
cmd = 'ls -al'
os.system(cmd)
If you want to return the results of the command, you can use os.popen
. However, this is deprecated since version 2.6 in favor of the subprocess module, which other answers have covered well.
回答 7
有很多不同的库,可让您使用Python调用外部命令。对于每个库,我都进行了描述并显示了调用外部命令的示例。我用作示例的命令是ls -l
(列出所有文件)。如果您想了解有关任何库的更多信息,我已列出并链接了每个库的文档。
资料来源:
这些都是库:
希望这可以帮助您决定使用哪个库:)
子过程
子进程允许您调用外部命令,并将其连接到它们的输入/输出/错误管道(stdin,stdout和stderr)。子进程是运行命令的默认选择,但有时其他模块更好。
subprocess.run(["ls", "-l"]) # Run command
subprocess.run(["ls", "-l"], stdout=subprocess.PIPE) # This will run the command and return any output
subprocess.run(shlex.split("ls -l")) # You can also use the shlex library to split the command
操作系统
os用于“取决于操作系统的功能”。它也可以用于通过os.system
和调用外部命令os.popen
(注意:还有一个subprocess.popen)。os将始终运行该shell,对于不需要或不知道如何使用的人来说,它是一个简单的选择subprocess.run
。
os.system("ls -l") # run command
os.popen("ls -l").read() # This will run the command and return any output
SH
sh是一个子进程接口,可让您像调用程序一样调用程序。如果要多次运行命令,这很有用。
sh.ls("-l") # Run command normally
ls_cmd = sh.Command("ls") # Save command as a variable
ls_cmd() # Run command as if it were a function
铅
plumbum是用于“类似脚本”的Python程序的库。您可以像中的函数那样调用程序sh
。如果您要在没有外壳的情况下运行管道,则Plumbum很有用。
ls_cmd = plumbum.local("ls -l") # get command
ls_cmd() # run command
期待
pexpect使您可以生成子应用程序,对其进行控制并在其输出中找到模式。对于在Unix上需要tty的命令,这是子过程的更好选择。
pexpect.run("ls -l") # Run command as normal
child = pexpect.spawn('scp foo user@example.com:.') # Spawns child application
child.expect('Password:') # When this is the output
child.sendline('mypassword')
布
fabric是一个Python 2.5和2.7库。它允许您执行本地和远程Shell命令。Fabric是在安全Shell(SSH)中运行命令的简单替代方案
fabric.operations.local('ls -l') # Run command as normal
fabric.operations.local('ls -l', capture = True) # Run command and receive output
使者
特使被称为“人类子过程”。它用作subprocess
模块周围的便利包装。
r = envoy.run("ls -l") # Run command
r.std_out # get output
命令
commands
包含的包装函数os.popen
,但由于subprocess
是更好的选择,它已从Python 3中删除。
该编辑基于JF Sebastian的评论。
There are lots of different libraries which allow you to call external commands with Python. For each library I’ve given a description and shown an example of calling an external command. The command I used as the example is ls -l
(list all files). If you want to find out more about any of the libraries I’ve listed and linked the documentation for each of them.
Sources:
These are all the libraries:
Hopefully this will help you make a decision on which library to use :)
subprocess
Subprocess allows you to call external commands and connect them to their input/output/error pipes (stdin, stdout, and stderr). Subprocess is the default choice for running commands, but sometimes other modules are better.
subprocess.run(["ls", "-l"]) # Run command
subprocess.run(["ls", "-l"], stdout=subprocess.PIPE) # This will run the command and return any output
subprocess.run(shlex.split("ls -l")) # You can also use the shlex library to split the command
os
os is used for “operating system dependent functionality”. It can also be used to call external commands with os.system
and os.popen
(Note: There is also a subprocess.popen). os will always run the shell and is a simple alternative for people who don’t need to, or don’t know how to use subprocess.run
.
os.system("ls -l") # run command
os.popen("ls -l").read() # This will run the command and return any output
sh
sh is a subprocess interface which lets you call programs as if they were functions. This is useful if you want to run a command multiple times.
sh.ls("-l") # Run command normally
ls_cmd = sh.Command("ls") # Save command as a variable
ls_cmd() # Run command as if it were a function
plumbum
plumbum is a library for “script-like” Python programs. You can call programs like functions as in sh
. Plumbum is useful if you want to run a pipeline without the shell.
ls_cmd = plumbum.local("ls -l") # get command
ls_cmd() # run command
pexpect
pexpect lets you spawn child applications, control them and find patterns in their output. This is a better alternative to subprocess for commands that expect a tty on Unix.
pexpect.run("ls -l") # Run command as normal
child = pexpect.spawn('scp foo user@example.com:.') # Spawns child application
child.expect('Password:') # When this is the output
child.sendline('mypassword')
fabric
fabric is a Python 2.5 and 2.7 library. It allows you to execute local and remote shell commands. Fabric is simple alternative for running commands in a secure shell (SSH)
fabric.operations.local('ls -l') # Run command as normal
fabric.operations.local('ls -l', capture = True) # Run command and receive output
envoy
envoy is known as “subprocess for humans”. It is used as a convenience wrapper around the subprocess
module.
r = envoy.run("ls -l") # Run command
r.std_out # get output
commands
commands
contains wrapper functions for os.popen
, but it has been removed from Python 3 since subprocess
is a better alternative.
The edit was based on J.F. Sebastian’s comment.
回答 8
我总是用fabric
这样的东西:
from fabric.operations import local
result = local('ls', capture=True)
print "Content:/n%s" % (result, )
但这似乎是一个很好的工具:sh
(Python子进程接口)。
看一个例子:
from sh import vgdisplay
print vgdisplay()
print vgdisplay('-v')
print vgdisplay(v=True)
I always use fabric
for this things like:
from fabric.operations import local
result = local('ls', capture=True)
print "Content:/n%s" % (result, )
But this seem to be a good tool: sh
(Python subprocess interface).
Look at an example:
from sh import vgdisplay
print vgdisplay()
print vgdisplay('-v')
print vgdisplay(v=True)
回答 9
还要检查“ pexpect” Python库。
它允许交互式控制外部程序/命令,甚至包括ssh,ftp,telnet等。您可以键入以下内容:
child = pexpect.spawn('ftp 192.168.0.24')
child.expect('(?i)name .*: ')
child.sendline('anonymous')
child.expect('(?i)password')
Check the “pexpect” Python library, too.
It allows for interactive controlling of external programs/commands, even ssh, ftp, telnet, etc. You can just type something like:
child = pexpect.spawn('ftp 192.168.0.24')
child.expect('(?i)name .*: ')
child.sendline('anonymous')
child.expect('(?i)password')
回答 10
与标准库
使用子流程模块(Python 3):
import subprocess
subprocess.run(['ls', '-l'])
这是推荐的标准方式。但是,构造和编写更复杂的任务(管道,输出,输入等)可能很繁琐。
关于Python版本的注意事项:如果您仍在使用Python 2,subprocess.call的工作方式与此类似。
ProTip:shlex.split可以帮助您解析,和其他功能的命令run
,以防您不想(或您不能!)以列表形式提供它们:call
subprocess
import shlex
import subprocess
subprocess.run(shlex.split('ls -l'))
具有外部依赖性
如果您不介意外部依赖性,请使用plumbum:
from plumbum.cmd import ifconfig
print(ifconfig['wlan0']())
这是最好的subprocess
包装纸。它是跨平台的,即可以在Windows和类似Unix的系统上运行。由安装pip install plumbum
。
另一个受欢迎的图书馆是sh:
from sh import ifconfig
print(ifconfig('wlan0'))
但是,已sh
放弃Windows支持,因此它不像以前那样出色。由安装pip install sh
。
With the standard library
Use the subprocess module (Python 3):
import subprocess
subprocess.run(['ls', '-l'])
It is the recommended standard way. However, more complicated tasks (pipes, output, input, etc.) can be tedious to construct and write.
Note on Python version: If you are still using Python 2, subprocess.call works in a similar way.
ProTip: shlex.split can help you to parse the command for run
, call
, and other subprocess
functions in case you don’t want (or you can’t!) provide them in form of lists:
import shlex
import subprocess
subprocess.run(shlex.split('ls -l'))
With external dependencies
If you do not mind external dependencies, use plumbum:
from plumbum.cmd import ifconfig
print(ifconfig['wlan0']())
It is the best subprocess
wrapper. It’s cross-platform, i.e. it works on both Windows and Unix-like systems. Install by pip install plumbum
.
Another popular library is sh:
from sh import ifconfig
print(ifconfig('wlan0'))
However, sh
dropped Windows support, so it’s not as awesome as it used to be. Install by pip install sh
.
回答 11
如果您需要从您所呼叫的命令的输出,那么你可以使用subprocess.check_output(Python的2.7+)。
>>> subprocess.check_output(["ls", "-l", "/dev/null"])
'crw-rw-rw- 1 root root 1, 3 Oct 18 2007 /dev/null\n'
还要注意shell参数。
如果shell是True
,则将通过Shell执行指定的命令。如果您主要将Python用于大多数系统外壳程序提供的增强控制流,并且仍希望方便地访问其他外壳程序功能(例如外壳程序管道,文件名通配符,环境变量扩展以及〜扩展到用户的家),则这可能很有用。目录。但是请注意,Python本身提供的实现多壳状的功能(尤其是glob
,fnmatch
,os.walk()
,os.path.expandvars()
,os.path.expanduser()
,和shutil
)。
If you need the output from the command you are calling,
then you can use subprocess.check_output (Python 2.7+).
>>> subprocess.check_output(["ls", "-l", "/dev/null"])
'crw-rw-rw- 1 root root 1, 3 Oct 18 2007 /dev/null\n'
Also note the shell parameter.
If shell is True
, the specified command will be executed through the shell. This can be useful if you are using Python primarily for the enhanced control flow it offers over most system shells and still want convenient access to other shell features such as shell pipes, filename wildcards, environment variable expansion, and expansion of ~ to a user’s home directory. However, note that Python itself offers implementations of many shell-like features (in particular, glob
, fnmatch
, os.walk()
, os.path.expandvars()
, os.path.expanduser()
, and shutil
).
回答 12
这就是我运行命令的方式。这段代码包含了您非常需要的所有内容
from subprocess import Popen, PIPE
cmd = "ls -l ~/"
p = Popen(cmd , shell=True, stdout=PIPE, stderr=PIPE)
out, err = p.communicate()
print "Return code: ", p.returncode
print out.rstrip(), err.rstrip()
This is how I run my commands. This code has everything you need pretty much
from subprocess import Popen, PIPE
cmd = "ls -l ~/"
p = Popen(cmd , shell=True, stdout=PIPE, stderr=PIPE)
out, err = p.communicate()
print "Return code: ", p.returncode
print out.rstrip(), err.rstrip()
回答 13
更新:
subprocess.run
如果您的代码不需要保持与早期Python版本的兼容性,则从python 3.5开始,建议使用此方法。它更加一致,并且提供与Envoy类似的易用性。(管道并不是那么简单。有关如何查看此问题的信息。)
以下是文档中的一些示例。
运行一个过程:
>>> subprocess.run(["ls", "-l"]) # Doesn't capture output
CompletedProcess(args=['ls', '-l'], returncode=0)
在失败的跑步上加薪:
>>> subprocess.run("exit 1", shell=True, check=True)
Traceback (most recent call last):
...
subprocess.CalledProcessError: Command 'exit 1' returned non-zero exit status 1
捕获输出:
>>> subprocess.run(["ls", "-l", "/dev/null"], stdout=subprocess.PIPE)
CompletedProcess(args=['ls', '-l', '/dev/null'], returncode=0,
stdout=b'crw-rw-rw- 1 root root 1, 3 Jan 23 16:23 /dev/null\n')
原始答案:
我建议尝试Envoy。它是子流程的包装,后者旨在替换较旧的模块和功能。特使是人类的子过程。
自述文件中的示例用法:
>>> r = envoy.run('git config', data='data to pipe in', timeout=2)
>>> r.status_code
129
>>> r.std_out
'usage: git config [options]'
>>> r.std_err
''
管道周围的东西:
>>> r = envoy.run('uptime | pbcopy')
>>> r.command
'pbcopy'
>>> r.status_code
0
>>> r.history
[<Response 'uptime'>]
Update:
subprocess.run
is the recommended approach as of Python 3.5 if your code does not need to maintain compatibility with earlier Python versions. It’s more consistent and offers similar ease-of-use as Envoy. (Piping isn’t as straightforward though. See this question for how.)
Here’s some examples from the documentation.
Run a process:
>>> subprocess.run(["ls", "-l"]) # Doesn't capture output
CompletedProcess(args=['ls', '-l'], returncode=0)
Raise on failed run:
>>> subprocess.run("exit 1", shell=True, check=True)
Traceback (most recent call last):
...
subprocess.CalledProcessError: Command 'exit 1' returned non-zero exit status 1
Capture output:
>>> subprocess.run(["ls", "-l", "/dev/null"], stdout=subprocess.PIPE)
CompletedProcess(args=['ls', '-l', '/dev/null'], returncode=0,
stdout=b'crw-rw-rw- 1 root root 1, 3 Jan 23 16:23 /dev/null\n')
Original answer:
I recommend trying Envoy. It’s a wrapper for subprocess, which in turn aims to replace the older modules and functions. Envoy is subprocess for humans.
Example usage from the README:
>>> r = envoy.run('git config', data='data to pipe in', timeout=2)
>>> r.status_code
129
>>> r.std_out
'usage: git config [options]'
>>> r.std_err
''
Pipe stuff around too:
>>> r = envoy.run('uptime | pbcopy')
>>> r.command
'pbcopy'
>>> r.status_code
0
>>> r.history
[<Response 'uptime'>]
回答 14
使用子过程。
…或一个非常简单的命令:
import os
os.system('cat testfile')
Use subprocess.
…or for a very simple command:
import os
os.system('cat testfile')
回答 15
在Python中调用外部命令
简单,使用subprocess.run
,它返回一个CompletedProcess
对象:
>>> import subprocess
>>> completed_process = subprocess.run('python --version')
Python 3.6.1 :: Anaconda 4.4.0 (64-bit)
>>> completed_process
CompletedProcess(args='python --version', returncode=0)
为什么?
从Python 3.5开始,文档建议使用subprocess.run:
推荐的调用子流程的方法是将run()函数用于它可以处理的所有用例。对于更高级的用例,可以直接使用基础Popen接口。
这是最简单的用法示例-完全按照要求进行:
>>> import subprocess
>>> completed_process = subprocess.run('python --version')
Python 3.6.1 :: Anaconda 4.4.0 (64-bit)
>>> completed_process
CompletedProcess(args='python --version', returncode=0)
run
等待命令成功完成,然后返回一个CompletedProcess
对象。相反,它可以引发TimeoutExpired
(如果您给它提供一个timeout=
参数)或CalledProcessError
(如果失败并通过check=True
)。
从上面的示例可以推断,默认情况下,stdout和stderr都通过管道传递到您自己的stdout和stderr。
我们可以检查返回的对象,并查看给出的命令和返回码:
>>> completed_process.args
'python --version'
>>> completed_process.returncode
0
捕获输出
如果要捕获输出,则可以传递subprocess.PIPE
给相应的stderr
或stdout
:
>>> cp = subprocess.run('python --version',
stderr=subprocess.PIPE,
stdout=subprocess.PIPE)
>>> cp.stderr
b'Python 3.6.1 :: Anaconda 4.4.0 (64-bit)\r\n'
>>> cp.stdout
b''
(我发现将版本信息放入stderr而不是stdout很有意思,并且有点违反直觉。)
传递命令清单
可以很容易地从手动提供命令字符串(如问题所提示)转变为以编程方式构建的字符串。不要以编程方式构建字符串。这是一个潜在的安全问题。最好假设您不信任输入。
>>> import textwrap
>>> args = ['python', textwrap.__file__]
>>> cp = subprocess.run(args, stdout=subprocess.PIPE)
>>> cp.stdout
b'Hello there.\r\n This is indented.\r\n'
注意,仅args
应按位置传递。
完整签名
这是源中的实际签名,如下所示help(run)
:
def run(*popenargs, input=None, timeout=None, check=False, **kwargs):
该popenargs
和kwargs
被给予Popen
的构造。input
可以是universal_newlines=True
将通过管道传输到子进程的stdin 的字节字符串(或unicode,如果指定encoding或)。
该文档描述了timeout=
和check=True
比我更可以:
超时参数传递给Popen.communicate()。如果超时到期,子进程将被终止并等待。子进程终止后,将重新引发TimeoutExpired异常。
如果check为true,并且进程以非零退出代码退出,则将引发CalledProcessError异常。该异常的属性包含参数,退出代码以及stdout和stderr(如果已捕获)。
这个示例check=True
比我能想到的更好:
>>> subprocess.run("exit 1", shell=True, check=True)
Traceback (most recent call last):
...
subprocess.CalledProcessError: Command 'exit 1' returned non-zero exit status 1
扩展签名
这是文档中提供的扩展签名:
subprocess.run(args, *, stdin=None, input=None, stdout=None, stderr=None,
shell=False, cwd=None, timeout=None, check=False, encoding=None,
errors=None)
请注意,这表明只应在位置传递args列表。因此,将其余参数作为关键字参数传递。
开普
何时使用Popen
代替?我将很难仅根据参数来找到用例。Popen
但是,直接使用poll
will 将使您能够访问其方法,包括,“ send_signal”,“ terminate”和“ wait”。
这是来源中Popen
给出的签名。我认为这是信息的最精确封装(与相对):help(Popen)
def __init__(self, args, bufsize=-1, executable=None,
stdin=None, stdout=None, stderr=None,
preexec_fn=None, close_fds=_PLATFORM_DEFAULT_CLOSE_FDS,
shell=False, cwd=None, env=None, universal_newlines=False,
startupinfo=None, creationflags=0,
restore_signals=True, start_new_session=False,
pass_fds=(), *, encoding=None, errors=None):
但更多的是信息的Popen
文档:
subprocess.Popen(args, bufsize=-1, executable=None, stdin=None,
stdout=None, stderr=None, preexec_fn=None, close_fds=True,
shell=False, cwd=None, env=None, universal_newlines=False,
startupinfo=None, creationflags=0, restore_signals=True,
start_new_session=False, pass_fds=(), *, encoding=None, errors=None)
在新进程中执行子程序。在POSIX上,该类使用类似于os.execvp()的行为来执行子程序。在Windows上,该类使用Windows CreateProcess()函数。Popen的参数如下。
剩下的内容Popen
将作为读者的练习。
Calling an external command in Python
Simple, use subprocess.run
, which returns a CompletedProcess
object:
>>> import subprocess
>>> completed_process = subprocess.run('python --version')
Python 3.6.1 :: Anaconda 4.4.0 (64-bit)
>>> completed_process
CompletedProcess(args='python --version', returncode=0)
Why?
As of Python 3.5, the documentation recommends subprocess.run:
The recommended approach to invoking subprocesses is to use the run() function for all use cases it can handle. For more advanced use cases, the underlying Popen interface can be used directly.
Here’s an example of the simplest possible usage – and it does exactly as asked:
>>> import subprocess
>>> completed_process = subprocess.run('python --version')
Python 3.6.1 :: Anaconda 4.4.0 (64-bit)
>>> completed_process
CompletedProcess(args='python --version', returncode=0)
run
waits for the command to successfully finish, then returns a CompletedProcess
object. It may instead raise TimeoutExpired
(if you give it a timeout=
argument) or CalledProcessError
(if it fails and you pass check=True
).
As you might infer from the above example, stdout and stderr both get piped to your own stdout and stderr by default.
We can inspect the returned object and see the command that was given and the returncode:
>>> completed_process.args
'python --version'
>>> completed_process.returncode
0
Capturing output
If you want to capture the output, you can pass subprocess.PIPE
to the appropriate stderr
or stdout
:
>>> cp = subprocess.run('python --version',
stderr=subprocess.PIPE,
stdout=subprocess.PIPE)
>>> cp.stderr
b'Python 3.6.1 :: Anaconda 4.4.0 (64-bit)\r\n'
>>> cp.stdout
b''
(I find it interesting and slightly counterintuitive that the version info gets put to stderr instead of stdout.)
Pass a command list
One might easily move from manually providing a command string (like the question suggests) to providing a string built programmatically. Don’t build strings programmatically. This is a potential security issue. It’s better to assume you don’t trust the input.
>>> import textwrap
>>> args = ['python', textwrap.__file__]
>>> cp = subprocess.run(args, stdout=subprocess.PIPE)
>>> cp.stdout
b'Hello there.\r\n This is indented.\r\n'
Note, only args
should be passed positionally.
Full Signature
Here’s the actual signature in the source and as shown by help(run)
:
def run(*popenargs, input=None, timeout=None, check=False, **kwargs):
The popenargs
and kwargs
are given to the Popen
constructor. input
can be a string of bytes (or unicode, if specify encoding or universal_newlines=True
) that will be piped to the subprocess’s stdin.
The documentation describes timeout=
and check=True
better than I could:
The timeout argument is passed to Popen.communicate(). If the timeout
expires, the child process will be killed and waited for. The
TimeoutExpired exception will be re-raised after the child process has
terminated.
If check is true, and the process exits with a non-zero exit code, a
CalledProcessError exception will be raised. Attributes of that
exception hold the arguments, the exit code, and stdout and stderr if
they were captured.
and this example for check=True
is better than one I could come up with:
>>> subprocess.run("exit 1", shell=True, check=True)
Traceback (most recent call last):
...
subprocess.CalledProcessError: Command 'exit 1' returned non-zero exit status 1
Expanded Signature
Here’s an expanded signature, as given in the documentation:
subprocess.run(args, *, stdin=None, input=None, stdout=None, stderr=None,
shell=False, cwd=None, timeout=None, check=False, encoding=None,
errors=None)
Note that this indicates that only the args list should be passed positionally. So pass the remaining arguments as keyword arguments.
Popen
When use Popen
instead? I would struggle to find use-case based on the arguments alone. Direct usage of Popen
would, however, give you access to its methods, including poll
, ‘send_signal’, ‘terminate’, and ‘wait’.
Here’s the Popen
signature as given in the source. I think this is the most precise encapsulation of the information (as opposed to help(Popen)
):
def __init__(self, args, bufsize=-1, executable=None,
stdin=None, stdout=None, stderr=None,
preexec_fn=None, close_fds=_PLATFORM_DEFAULT_CLOSE_FDS,
shell=False, cwd=None, env=None, universal_newlines=False,
startupinfo=None, creationflags=0,
restore_signals=True, start_new_session=False,
pass_fds=(), *, encoding=None, errors=None):
But more informative is the Popen
documentation:
subprocess.Popen(args, bufsize=-1, executable=None, stdin=None,
stdout=None, stderr=None, preexec_fn=None, close_fds=True,
shell=False, cwd=None, env=None, universal_newlines=False,
startupinfo=None, creationflags=0, restore_signals=True,
start_new_session=False, pass_fds=(), *, encoding=None, errors=None)
Execute a child program in a new process. On POSIX, the class uses
os.execvp()-like behavior to execute the child program. On Windows,
the class uses the Windows CreateProcess() function. The arguments to
Popen are as follows.
Understanding the remaining documentation on Popen
will be left as an exercise for the reader.
回答 16
os.system
可以,但是有点过时。这也不是很安全。相反,请尝试subprocess
。 subprocess
不会直接调用sh,因此比os.system
。
在此处获取更多信息。
os.system
is OK, but kind of dated. It’s also not very secure. Instead, try subprocess
. subprocess
does not call sh directly and is therefore more secure than os.system
.
Get more information here.
回答 17
还有铅
>>> from plumbum import local
>>> ls = local["ls"]
>>> ls
LocalCommand(<LocalPath /bin/ls>)
>>> ls()
u'build.py\ndist\ndocs\nLICENSE\nplumbum\nREADME.rst\nsetup.py\ntests\ntodo.txt\n'
>>> notepad = local["c:\\windows\\notepad.exe"]
>>> notepad() # Notepad window pops up
u'' # Notepad window is closed by user, command returns
There is also Plumbum
>>> from plumbum import local
>>> ls = local["ls"]
>>> ls
LocalCommand(<LocalPath /bin/ls>)
>>> ls()
u'build.py\ndist\ndocs\nLICENSE\nplumbum\nREADME.rst\nsetup.py\ntests\ntodo.txt\n'
>>> notepad = local["c:\\windows\\notepad.exe"]
>>> notepad() # Notepad window pops up
u'' # Notepad window is closed by user, command returns
回答 18
采用:
import os
cmd = 'ls -al'
os.system(cmd)
os-此模块提供了使用依赖于操作系统的功能的可移植方式。
对于更多的os
功能,这里是文档。
Use:
import os
cmd = 'ls -al'
os.system(cmd)
os – This module provides a portable way of using operating system-dependent functionality.
For the more os
functions, here is the documentation.
回答 19
可能很简单:
import os
cmd = "your command"
os.system(cmd)
It can be this simple:
import os
cmd = "your command"
os.system(cmd)
回答 20
我非常喜欢shell_command的简单性。它建立在子流程模块的顶部。
这是文档中的示例:
>>> from shell_command import shell_call
>>> shell_call("ls *.py")
setup.py shell_command.py test_shell_command.py
0
>>> shell_call("ls -l *.py")
-rw-r--r-- 1 ncoghlan ncoghlan 391 2011-12-11 12:07 setup.py
-rw-r--r-- 1 ncoghlan ncoghlan 7855 2011-12-11 16:16 shell_command.py
-rwxr-xr-x 1 ncoghlan ncoghlan 8463 2011-12-11 16:17 test_shell_command.py
0
I quite like shell_command for its simplicity. It’s built on top of the subprocess module.
Here’s an example from the documentation:
>>> from shell_command import shell_call
>>> shell_call("ls *.py")
setup.py shell_command.py test_shell_command.py
0
>>> shell_call("ls -l *.py")
-rw-r--r-- 1 ncoghlan ncoghlan 391 2011-12-11 12:07 setup.py
-rw-r--r-- 1 ncoghlan ncoghlan 7855 2011-12-11 16:16 shell_command.py
-rwxr-xr-x 1 ncoghlan ncoghlan 8463 2011-12-11 16:17 test_shell_command.py
0
回答 21
这里还有另一个以前没有提到的区别。
subprocess.Popen
将<command>作为子进程执行。就我而言,我需要执行文件<a>,该文件需要与另一个程序<b>通信。
我尝试了子流程,执行成功。但是<b>无法与<a>通信。当我从终端运行时,一切都正常。
还有一个:(注意:kwrite的行为与其他应用程序不同。如果在Firefox上尝试以下操作,结果将有所不同。)
如果尝试这样做os.system("kwrite")
,程序流将冻结,直到用户关闭kwrite。为了克服这个问题,我改为尝试os.system(konsole -e kwrite)
。这个时间程序继续进行,但是kwrite成为了控制台的子进程。
任何人都运行kwrite而不是子进程(即,它必须在系统监视器中显示在树的最左侧)。
There is another difference here which is not mentioned previously.
subprocess.Popen
executes the <command> as a subprocess. In my case, I need to execute file <a> which needs to communicate with another program, <b>.
I tried subprocess, and execution was successful. However <b> could not communicate with <a>.
Everything is normal when I run both from the terminal.
One more:
(NOTE: kwrite behaves different from other applications. If you try the below with Firefox, the results will not be the same.)
If you try os.system("kwrite")
, program flow freezes until the user closes kwrite. To overcome that I tried instead os.system(konsole -e kwrite)
. This time program continued to flow, but kwrite became the subprocess of the console.
Anyone runs the kwrite not being a subprocess (i.e. in the system monitor it must appear at the leftmost edge of the tree).
回答 22
os.system
不允许您存储结果,因此,如果要将结果存储在某个列表或其他内容中,则subprocess.call
可以使用。
os.system
does not allow you to store results, so if you want to store results in some list or something, a subprocess.call
works.
回答 23
subprocess.check_call
如果您不想测试返回值,则非常方便。任何错误都会引发异常。
subprocess.check_call
is convenient if you don’t want to test return values. It throws an exception on any error.
回答 24
我倾向于将子进程与shlex一起使用(以处理带引号的字符串的转义):
>>> import subprocess, shlex
>>> command = 'ls -l "/your/path/with spaces/"'
>>> call_params = shlex.split(command)
>>> print call_params
["ls", "-l", "/your/path/with spaces/"]
>>> subprocess.call(call_params)
I tend to use subprocess together with shlex (to handle escaping of quoted strings):
>>> import subprocess, shlex
>>> command = 'ls -l "/your/path/with spaces/"'
>>> call_params = shlex.split(command)
>>> print call_params
["ls", "-l", "/your/path/with spaces/"]
>>> subprocess.call(call_params)
回答 25
无耻的插件,我为此写了一个库:P
https://github.com/houqp/shell.py
目前,它基本上是popen和shlex的包装。它还支持管道命令,因此您可以在Python中更轻松地链接命令。因此,您可以执行以下操作:
ex('echo hello shell.py') | "awk '{print $2}'"
Shameless plug, I wrote a library for this :P
https://github.com/houqp/shell.py
It’s basically a wrapper for popen and shlex for now. It also supports piping commands so you can chain commands easier in Python. So you can do things like:
ex('echo hello shell.py') | "awk '{print $2}'"
回答 26
在Windows中你可以导入subprocess
模块,并通过调用运行外部命令subprocess.Popen()
,subprocess.Popen().communicate()
并subprocess.Popen().wait()
如下:
# Python script to run a command line
import subprocess
def execute(cmd):
"""
Purpose : To execute a command and return exit status
Argument : cmd - command to execute
Return : exit_code
"""
process = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
(result, error) = process.communicate()
rc = process.wait()
if rc != 0:
print "Error: failed to execute command:", cmd
print error
return result
# def
command = "tasklist | grep python"
print "This process detail: \n", execute(command)
输出:
This process detail:
python.exe 604 RDP-Tcp#0 4 5,660 K
In Windows you can just import the subprocess
module and run external commands by calling subprocess.Popen()
, subprocess.Popen().communicate()
and subprocess.Popen().wait()
as below:
# Python script to run a command line
import subprocess
def execute(cmd):
"""
Purpose : To execute a command and return exit status
Argument : cmd - command to execute
Return : exit_code
"""
process = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
(result, error) = process.communicate()
rc = process.wait()
if rc != 0:
print "Error: failed to execute command:", cmd
print error
return result
# def
command = "tasklist | grep python"
print "This process detail: \n", execute(command)
Output:
This process detail:
python.exe 604 RDP-Tcp#0 4 5,660 K
回答 27
在Linux下,如果您想调用将独立执行的外部命令(在python脚本终止后将继续运行),则可以使用简单的队列作为任务假脱机程序或at命令
任务假脱机程序的示例:
import os
os.system('ts <your-command>')
有关任务后台处理程序(ts
)的注意事项:
您可以使用以下命令设置要运行的并发进程数(“插槽”):
ts -S <number-of-slots>
安装ts
不需要管理员权限。您可以使用简单的源代码下载并编译它make
,然后将其添加到路径中,即可完成操作。
Under Linux, in case you would like to call an external command that will execute independently (will keep running after the python script terminates), you can use a simple queue as task spooler or the at command
An example with task spooler:
import os
os.system('ts <your-command>')
Notes about task spooler (ts
):
You could set the number of concurrent processes to be run (“slots”) with:
ts -S <number-of-slots>
Installing ts
doesn’t requires admin privileges. You can download and compile it from source with a simple make
, add it to your path and you’re done.
回答 28
您可以使用Popen,然后可以检查过程的状态:
from subprocess import Popen
proc = Popen(['ls', '-l'])
if proc.poll() is None:
proc.kill()
签出subprocess.Popen。
You can use Popen, and then you can check the procedure’s status:
from subprocess import Popen
proc = Popen(['ls', '-l'])
if proc.poll() is None:
proc.kill()
Check out subprocess.Popen.
回答 29
要从OpenStack Neutron获取网络ID :
#!/usr/bin/python
import os
netid = "nova net-list | awk '/ External / { print $2 }'"
temp = os.popen(netid).read() /* Here temp also contains new line (\n) */
networkId = temp.rstrip()
print(networkId)
nova net-list的输出
+--------------------------------------+------------+------+
| ID | Label | CIDR |
+--------------------------------------+------------+------+
| 431c9014-5b5d-4b51-a357-66020ffbb123 | test1 | None |
| 27a74fcd-37c0-4789-9414-9531b7e3f126 | External | None |
| 5a2712e9-70dc-4b0e-9281-17e02f4684c9 | management | None |
| 7aa697f5-0e60-4c15-b4cc-9cb659698512 | Internal | None |
+--------------------------------------+------------+------+
打印输出(networkId)
27a74fcd-37c0-4789-9414-9531b7e3f126
To fetch the network id from the OpenStack Neutron:
#!/usr/bin/python
import os
netid = "nova net-list | awk '/ External / { print $2 }'"
temp = os.popen(netid).read() /* Here temp also contains new line (\n) */
networkId = temp.rstrip()
print(networkId)
Output of nova net-list
+--------------------------------------+------------+------+
| ID | Label | CIDR |
+--------------------------------------+------------+------+
| 431c9014-5b5d-4b51-a357-66020ffbb123 | test1 | None |
| 27a74fcd-37c0-4789-9414-9531b7e3f126 | External | None |
| 5a2712e9-70dc-4b0e-9281-17e02f4684c9 | management | None |
| 7aa697f5-0e60-4c15-b4cc-9cb659698512 | Internal | None |
+--------------------------------------+------------+------+
Output of print(networkId)
27a74fcd-37c0-4789-9414-9531b7e3f126