python调用系统中应用程序的函数示例
作者:jiangwei0512 时间:2021-01-18 11:06:32
os.system()
在shell中执行一条命令。函数原型如下:
它是最简单的调用系统应用的方式,下面是一个例子:
import os
import sys
os.system("dir")
os.system("git")
结果如下(在VS Code中的打印结果,后面略了一部分打印信息):
D:\Codes\python_everything>cd d:\Codes\python_everything && cmd /C "set "PYTHONIOENCODING=UTF-8" && set "PYTHONUNBUFFERED=1" && C:\Python36\python.exe c:\Users\HOME\.vscode\extensions\ms-python.python-2018.6.0\pythonFiles\PythonTools\visualstudio_py_launcher.py d:\Codes\python_everything 56010 34806ad9-833a-4524-8cd6-18ca4aa74f14 RedirectOutput,RedirectOutput d:\Codes\python_everything\python-notes\WindowsAppCall.py "
驱动器 D 中的卷是 DATA1
卷的序列号是 A473-C4EE
D:\Codes\python_everything 的目录
2018/07/01 00:57 <DIR> .
2018/07/01 00:57 <DIR> ..
2018/06/11 22:09 1,332 .gitignore
2018/06/11 20:57 <DIR> .vscode
2018/06/12 00:27 <DIR> begining-python
2018/07/01 00:58 <DIR> python-notes
2018/07/01 00:57 <DIR> python-tools
2018/06/21 23:06 204 README.md
2 个文件 1,536 字节
6 个目录 235,383,660,544 可用字节
usage: git [--version] [--help] [-C <path>] [-c <name>=<value>]
[--exec-path[=<path>]] [--html-path] [--man-path] [--info-path]
[-p | --paginate | --no-pager] [--no-replace-objects] [--bare]
[--git-dir=<path>] [--work-tree=<path>] [--namespace=<name>]
<command> [<args>]
These are common Git commands used in various situations:
调用该函数直接将返回结果返回,所以就直接打印出来了。
os.popen()
也是在Shell下执行一条指令,但是它会返回一个文件描述符,这个文件描述符对应到管道对象,可以从中读取命令执行的返回信息。函数原型如下:
下面是一个例子:
import os
import sys
info = os.popen("dir")
print(info.readline())
print(info.readline())
print(info.readline())
print(info.readline())
print(info.readline())
因为popen返回的是一个文件对象,因此可以去读,也可以进行其它的很多操作。
不仅可以读,还可以写,不过需要设定popen的第二个参数mode为'w'。
commands.getstatusoutput()
这个在执行完指令之后会同时返回状态值和命令输出,不过它似乎无法区分stdout和stderr,所以在3.x版本中被废掉了,这里不再介绍。
subprocess.call()
也是在Shell下执行指令,整个subprocess模块是一个比较完备的模块,是用来代替os.system()等的。
call函数的原型如下:
这里举一个简单的例子:
import subprocess
subprocess.call('git')
关于subprocess模块的总体介绍,见
17.5. subprocess — Subprocess management — Python 3.6.15 documentation。
来源:https://blog.csdn.net/jiangwei0512/article/details/80861733
标签:python,程序调用,系统调用,函数
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
想用户所想(感受亚马逊的设计)
2007-08-26 17:09:00
![](https://img.aspxhome.com/file/UploadPic/20078/26/2007826171322368s.gif)
python time.strptime格式化实例详解
2022-03-25 19:13:02
python 子类调用父类的构造函数实例
2023-05-27 22:33:37
![](https://img.aspxhome.com/file/2023/5/70135_0s.jpg)
asp中InstrRev的语法
2008-01-22 18:14:00
ASP生成静态页面的方法
2010-05-27 12:13:00
理解SQL SERVER中的逻辑读,预读和物理读
2012-01-05 19:32:29
让文本框textarea自动适应内容的高度
2008-07-01 14:44:00
Yii2 rbac权限控制之菜单menu实例教程
2023-11-14 10:41:19
![](https://img.aspxhome.com/file/2023/1/88281_0s.png)
python打印日志方法的使用教程(logging模块)
2023-12-10 01:39:14
![](https://img.aspxhome.com/file/2023/6/86576_0s.png)
理清mysql外链的含义和命令
2010-03-09 15:55:00
python基础while循环及if判断的实例讲解
2021-02-18 06:56:06
原来CSS也可以把IE6弄死
2007-08-14 09:30:00
JavaScript setTimeout和setInterval的使用方法 说明
2023-08-31 10:48:19
恢复master..xp_logattach(log explorer)
2010-07-01 19:19:00
Python 中的参数传递、返回值、浅拷贝、深拷贝
2022-10-12 12:59:16
微信小程序上传图片到php服务器的方法
2023-11-07 11:57:25
python爬取淘宝商品销量信息
2023-06-01 14:59:03
PHP中round()函数对浮点数进行四舍五入的方法
2023-11-23 21:35:24
详解用Python为直方图绘制拟合曲线的两种方法
2021-06-15 23:58:08
![](https://img.aspxhome.com/file/2023/0/67350_0s.jpg)
简单了解python中的与或非运算
2021-03-20 01:45:02
![](https://img.aspxhome.com/file/2023/5/67915_0s.png)