python程序中调用其他程序的实现
作者:y6y6y666 时间:2021-08-07 12:33:16
1.os.system函数
wget 是一个下载软件的程序,如果已经下载好该软件,可以用py调用该软件。假如该软件目录在d:\tools\wget
import os
cmd =r'd:\tools\wget http://mirrors.sohu.com/nginx/nginx-1.13.9.zip'
os.system(cmd)
print('安装完毕')
实际上有三个程序:
py、shell、wget
py程序在哪里就下载到那里。
缺点:1.只有调用程序执行完后才能向下运行,不能获取页面内容。
解决以上两种缺点可以使用subprocess模块。
2.subprocess模块
可以获取内容。比如获取磁盘使用情况。
subprocess中的Popen类。照着视频写了一遍,好像是权限不够,就在管理员终端运行了,但是因为视频的格式输出和我的不一样所以切割方式也不一样,经过反复调试,终于切割出来了。
from subprocess import PIPE,Popen
#返回的是Popen实例对象
proc=Popen(
'fsutil volume diskfree c:',
stdin=None,
stdout=PIPE,
stderr=PIPE,
shell=True)
#communicate方法返回 输出到 标准输出 和标准错误 的字节串内容
#标准输出设备和标准错误设备 当前都是本终端设备
#获取输出和错误
outinfo,errinfo=proc.communicate()
#注意返回的内容是bytes 不是 str,解码,将机器码解码为字符串
outinfo =outinfo.decode('gbk')
errinfo =errinfo.decode('gbk')
print(outinfo)
print('------------')
print(errinfo)
#结果分行输出
outputList=outinfo.splitlines()
print(outputList)
#剩余量
#第0(1)行,split用于切片,replace用于重写,strip()用于将字符串的首尾中空格删除
free=int(outputList[0].split(':')[1].replace(',',"").strip().split(' ')[0])
#总空间
total=int(outputList[1].split(':')[1].replace(',',"").strip().split(' ')[0])
print('使用百分比:{:.2%}'.format(free/total))
if(free/total<0.1):
print('!!剩余空间告急!!!')
else:
print('剩余空间足够')
终于写完了。。。。。。。。。。。
subprocess可以同实进行下边的任务。
from subprocess import Popen
proc=Popen(
args='wget http://mirrors.sohu.com/nginx/nginx-1.13.9.zip',
shell=True
)
print('让他下载,我们先作其他的的事情')
来源:https://blog.csdn.net/m0_74020775/article/details/128807648
标签:python,调用其他程序
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
SQLServer中的切割字符串SplitString函数
2011-12-01 08:14:09
python安装sklearn模块的方法详解
2023-08-01 17:23:48
![](https://img.aspxhome.com/file/2023/9/61969_0s.jpg)
使用WingPro 7 设置Python路径的方法
2022-05-23 03:59:56
![](https://img.aspxhome.com/file/2023/5/69005_0s.jpg)
ASP链接Mysql数据库 非DSN连接的方法
2009-03-09 18:24:00
Python 字符串、列表、元组的截取与切片操作示例
2021-12-10 09:47:34
python实现控制COM口的示例
2023-01-01 03:57:05
pandas创建新Dataframe并添加多行的实例
2021-06-21 07:11:48
深入了解Golang中Slice切片的使用
2023-09-25 08:59:08
![](https://img.aspxhome.com/file/2023/9/87609_0s.png)
jupyter notebook中图片显示不出来的解决
2023-12-16 20:44:54
![](https://img.aspxhome.com/file/2023/2/133642_0s.jpg)
用python读写excel的方法
2021-11-11 10:10:50
Python文件操作函数用法实例详解
2021-03-12 07:10:29
matplotlib subplot绘制多个子图的方法示例
2021-12-18 14:49:50
![](https://img.aspxhome.com/file/2023/1/128761_0s.png)
Python虚拟环境virtualenv创建及使用过程图解
2023-05-18 10:57:22
![](https://img.aspxhome.com/file/2023/0/112970_0s.png)
Django 多语言教程的实现(i18n)
2022-12-18 22:41:39
![](https://img.aspxhome.com/file/2023/1/98811_0s.jpg)
决策树的python实现方法
2023-04-22 04:25:02
python使用 request 发送表单数据操作示例
2022-08-06 07:31:07
![](https://img.aspxhome.com/file/2023/0/131250_0s.png)
关于python中time和datetime的区别与用法
2022-07-15 00:54:17
详尽讲述用Python的Django框架测试驱动开发的教程
2023-04-21 18:28:19
![](https://img.aspxhome.com/file/2023/1/75921_0s.png)
python实现linux下使用xcopy的方法
2022-08-16 07:52:09
python通过http上传文件思路详解
2022-02-08 12:48:30
![](https://img.aspxhome.com/file/2023/9/79419_0s.png)