In this tutorial we will learn about Python System Command. Previously we learned about
Python Random Number
.
在本教程中,我们将学习Python系统命令。 先前我们了解了
Python随机数
。
Python系统命令
(
Python System Command
)
While making a program in python, you may need to exeucte some shell commands for your program. For example, if you use
Pycharm
IDE, you may notice that there is option to share your project on github. And you probably know that file transferring is done by
git
, which is operated using command line. So, Pycharm executes some shell commands in background to do it.
使用python编写程序时,您可能需要为程序执行一些shell命令。 例如,如果您使用
Pycharm
IDE,您可能会注意到可以在github上共享您的项目。 您可能知道文件传输是通过
git
完成的,而
git
是使用命令行操作的。 因此,Pycharm在后台执行一些shell命令来执行此操作。
However, In this tutorial we will learn some basics about executing shell commands from your python code.
但是,在本教程中,我们将学习一些有关从python代码执行shell命令的基础知识。
Python os.system()函数
(
Python os.system() function
)
We can execute system command by using
os.system()
function. According to the official document, it has been said that
我们可以使用
os.system()
函数执行系统命令。 根据官方文件,据说
This is implemented by calling the Standard C function system(), and has the same limitations.
这是通过调用标准C函数system()来实现的,并且具有相同的限制。
However, if command generates any output, it is sent to the interpreter standard output stream. Using this command is not recommended. In the following code we will try to know the version of git using the system command
git --version
.
但是,如果命令生成任何输出,则将其发送到解释器标准输出流。 不建议使用此命令。 在以下代码中,我们将尝试使用系统命令
git --version
了解
git --version
。
import os
cmd = "git --version"
returned_value = os.system(cmd) # returns the exit code in unix
print('returned value:', returned_value)
The following output found in ubuntu 16.04 where git is installed already.
在已安装git的ubuntu 16.04中找到以下输出。
git version 2.14.2
returned value: 0
Notice that we are not printing the git version command output to console, it’s being printed because console is the standard output stream here.
注意,我们没有将git version命令输出打印到控制台,因为控制台是此处的标准输出流,所以正在打印它。
Python subprocess.call()函数
(
Python subprocess.call() Function
)
In the previous section, we saw that
os.system()
function works fine. But it’s not recommended way to execute shell commands. We will use Python
subprocess
module to execute system commands.
在上一节中,我们看到
os.system()
函数可以正常工作。 但是不建议您执行Shell命令。 我们将使用Python
子进程
模块执行系统命令。
We can run shell commands by using
subprocess.call()
function. See the following code which is equivalent to the previous code.
我们可以使用
subprocess.call()
函数运行shell命令。 请参见以下代码,该代码与先前的代码等效。
import subprocess
cmd = "git --version"
returned_value = subprocess.call(cmd, shell=True) # returns the exit code in unix
print('returned value:', returned_value)
And the output will be same also.
并且输出也将相同。
Python subprocess.check_output()函数
(
Python subprocess.check_output() function
)
So far, we executed the system commands with the help of python. But we could not manipulate the output produced by those commands. Using
subprocess.check_output()
function we can store the output in a variable.
到目前为止,我们已经在python的帮助下执行了系统命令。 但是我们无法操纵这些命令产生的输出。 使用
subprocess.check_output()
函数,我们可以将输出存储在变量中。
import subprocess
cmd = "date"
# returns output as byte string
returned_output = subprocess.check_output(cmd)
# using decode() function to convert byte string to string
print('Current date is:', returned_output.decode("utf-8"))
It will produce output like the following
它将产生如下输出
Current date is: Thu Oct 5 16:31:41 IST 2017
So, in the above sections we have discussed about basic ideas about executing python system command. But there is no limit in learning. If you wish, you can learn more about Python System command using subprocess module from
official documentation
.
因此,在以上各节中,我们讨论了有关执行python系统命令的基本思想。 但是学习没有限制。 如果愿意,您可以从
官方文档中
了解有关使用子进程模块的Python系统命令的更多信息。
翻译自:
https://www.journaldev.com/16140/python-system-command-os-subprocess-call