網站首頁 編程語言 正文
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
相關推薦
- 2023-07-28 elementui 通過事件觸發動畫
- 2022-05-04 ?分享Python?中的?7?種交叉驗證方法_python
- 2022-09-29 React?Native?中處理?Android?手機吞字的解決方案_React
- 2022-12-15 Tensorflow2.1?MNIST圖像分類實現思路分析_python
- 2022-07-12 用戶手抖,連續點了兩次?優雅解決表單重復提交
- 2022-10-23 C#泛型集合類型實現添加和遍歷_C#教程
- 2023-07-04 spring boot security自定義認證
- 2022-08-18 C#實現從位圖到布隆過濾器的方法_C#教程
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支