日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

python popen函数讲解_Python常用模块函数代码汇总解析

發布時間:2024/10/8 python 24 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python popen函数讲解_Python常用模块函数代码汇总解析 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

一、文件和目錄操作

創建、刪除、修改、拼接、獲取當前目錄、遍歷目錄下的文件、獲取文件大小、修改日期、判斷文件是否存在等。略

二、日期和時間(內置模塊:time、datatime、calendar)

1.time.time() #返回自1970年1月1日0點到當前時間經過的秒數

實例1:獲取某函數執行的時間,單位秒

import time

before = time.time()

func1

after = time.time()

print(f"調用func1,花費時間{after-before}")

2.datetime.now() #返回當前時間對應的字符串

from datetime import datetime

print(datetime.now())

輸出結果:2020-06-27 15:48:38.400701

3.以指定格式顯示字符串

datetime.now().strftime('%Y-%m-%d -- %H:%M:%S')

time.strftime('%Y-%m-%d %H:%M:%S',time.localtime())

三、python程序中調用其他程序

python中調用外部程序,使用標準庫os庫的system函數、或者subproprocess庫。

1.wget(wget是一個從網絡上自動下載文件的自由工具,支持通過 HTTP、HTTPS、FTP 三個最常見的 TCP/IP協議下載)

1)mac上安裝wget命令:brew install wget

2)wget --help/wget -h

3)使用wget下載文件,下載文件至當前目錄下,mac終端命令:wget http://mirrors.sohu.com/nginx/nginx-1.13.9.zip

2.os.system函數

1)os.system調用外部程序,必須等被調用程序執行結束,才能繼續往下執行

2)os.system 函數沒法獲取 被調用程序輸出到終端窗口的內容

import os

cmd = 'wget http://mirrors.sohu.com/nginx/nginx-1.13.9.zip'

os.system(cmd)

---

version = input('請輸入安裝包版本:')

cmd = fr'd:\tools\wget http://mirrors.sohu.com/nginx/nginx-{version}.zip'

os.system(cmd)

3.subprocess模塊

實例1:將本該在終端輸出的信息用pipe獲取,并進行分析

from subprocess import PIPE, Popen

# 返回的是 Popen 實例對象

proc = Popen(

'du -sh *',

stdin = None,

stdout = PIPE,

stderr = PIPE,

shell=True)

outinfo, errinfo = proc.communicate() # communicate 方法返回 輸出到 標準輸出 和 標準錯誤 的字節串內容

outinfo = outinfo.decode('gbk')

errinfo = errinfo.decode('gbk')

outputList = outinfo.splitlines()

print(outputList[0].split(' ')[0].strip())

實例2:啟動wget下載文件

from subprocess import Popen

proc = Popen(

args='wget http://xxxxserver/xxxx.zip',

shell=True

)

使用subprocess不需要等外部程序執行結束,可以繼續執行其他程序

四、多線程

如果是自動化測試用例編寫,可以使用pytest測試框架,自帶多線程實現方法。

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持python博客。

與50位技術專家面對面20年技術見證,附贈技術全景圖

總結

以上是生活随笔為你收集整理的python popen函数讲解_Python常用模块函数代码汇总解析的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。