Python系统命令– os.system(),subprocess.call()

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 System Command

并且输出也将相同。

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

这个错误提示表明在尝试运行位于`/opt/todesk/bin/ToDesk`的程序时,系统找不到名为`libGL.so.1`的共享库文件。`libGL.so.1`通常用于OpenGL图形处理,可能是ToDesk依赖的一个关键库。 具体原因可能包括: 1. **缺失或损坏的库文件**:libGL.so.1可能没有正确安装,或者已损坏,导致程序无法找到它。 2. **链接路径问题**:ToDesk的执行文件可能没有设置正确的库搜索路径(LD_LIBRARY_PATH或DYLD_LIBRARY_PATH),所以系统无法在默认位置找到该文件。 3. **安装环境不一致**:如果ToDesk是在不同的环境中(如不同的Linux发行版或独立的桌面环境)安装的,可能导致库文件版本不兼容。 解决这个问题的一般步骤包括: 1. **检查库文件是否缺失**:尝试在系统的其他地方(比如标准库路径`/usr/lib`或`/usr/lib64`)查找`libGL.so.1`。 2. **重新安装相关库**:如果库文件缺失,确保已经安装了OpenGL和相关的图形库,根据你的系统可能需要运行`sudo apt-get install mesa-utils`(对于基于Debian的系统)或`yum install mesa-libGL`(对于基于RPM的系统)等命令。 3. **设置链接路径**:编辑ToDesk的启脚本或环境变量,确保`LD_LIBRARY_PATH`包含`libGL.so.1`所在的正确路径。 4. **确认兼容性**:如果使用的是特定版本的ToDesk,确保它与你的系统环境兼容。 如果你是开发者,还需要检查ToDesk的依赖声明,确保其明确指出了所需的库版本和依赖关系。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值