Python使用shutil操作文件、subprocess運(yùn)行子程序
一、shutil模塊(了解):高級的文件、文件夾、壓縮包處理模塊。
import shutil
# shutil.copyfileobj(fsrc, fdst[, length]),將文件內(nèi)容拷貝到另一個文件中
shutil.copyfileobj(open('old.xml', 'r'), open('new.xml', 'w'))
# shutil.copyfile(src, dst),拷貝文件
shutil.copyfile('f1.log', 'f2.log') # 目標(biāo)文件無需存在
# shutil.copymode(src, dst),僅拷貝權(quán)限。內(nèi)容、組、用戶均不變
shutil.copymode('f1.log', 'f2.log') # 目標(biāo)文件必須存在
# shutil.copystat(src, dst),僅拷貝狀態(tài)的信息,包括:mode bits, atime, mtime, flags
shutil.copystat('f1.log', 'f2.log') # 目標(biāo)文件必須存在
# shutil.copy(src, dst),拷貝文件和權(quán)限
shutil.copy('f1.log', 'f2.log')
# shutil.copy2(src, dst),拷貝文件和狀態(tài)信息
shutil.copy2('f1.log', 'f2.log')
# shutil.ignore_patterns(*patterns)
# shutil.copytree(src, dst, symlinks=False, ignore=None),遞歸的去拷貝文件夾
# 目標(biāo)目錄不能存在,注意對folder2目錄父級目錄要有可寫權(quán)限,ignore的意思是排除
shutil.copytree('folder1', 'folder2', ignore=shutil.ignore_patterns('*.pyc', 'tmp*'))
# shutil.rmtree(path[, ignore_errors[, onerror]]),遞歸的去刪除文件
shutil.rmtree('folder1')
# shutil.move(src, dst),遞歸的去移動文件,它類似mv命令,其實就是重命名
shutil.move('folder1', 'folder3')
# shutil.make_archive(base_name, format, ...),創(chuàng)建壓縮包并返回文件路徑,例如:zip、tar
'''
base_name: 壓縮包的文件名,也可以是壓縮包的路徑。只是文件名時,則保存至當(dāng)前目錄,否則保存至指定路徑,如 data_bak = >保存至當(dāng)前路徑;/ tmp/data_bak = >保存至/tmp/
format:壓縮包種類,“zip”, “tar”, “bztar”,“gztar”
root_dir:要壓縮的文件夾路徑(默認(rèn)當(dāng)前目錄)
owner:用戶,默認(rèn)當(dāng)前用戶
group:組,默認(rèn)當(dāng)前組
logger:用于記錄日志,通常是logging.Logger對象
'''
# 將 /data 下的文件打包放置當(dāng)前程序目錄
ret = shutil.make_archive("data_bak", 'gztar', root_dir='/data')
# 將 /data下的文件打包放置 /tmp/目錄
ret = shutil.make_archive("/tmp/data_bak", 'gztar', root_dir='/data')shutil 對壓縮包的處理是調(diào)用 ZipFile 和 TarFile 兩個模塊來進(jìn)行的,詳細(xì):
1、 zipfile壓縮解壓縮
import zipfile
# 壓縮
z = zipfile.ZipFile('laxi.zip', 'w')
z.write('a.log')
z.write('data.data')
z.close()
# 解壓
z = zipfile.ZipFile('laxi.zip', 'r')
z.extractall(path='.')
z.close()2、 tarfile壓縮解壓縮
import tarfile
# 壓縮
t=tarfile.open('/tmp/egon.tar','w')
t.add('/test1/a.py',arcname='a.bak')
t.add('/test1/b.py',arcname='b.bak')
t.close()
# 解壓
t=tarfile.open('/tmp/egon.tar','r')
t.extractall('/egon')
t.close()二、subprocess模塊(了解):運(yùn)行子程序
subprocess模塊允許你去創(chuàng)建一個新的進(jìn)程讓其執(zhí)行另外的程序,并與它進(jìn)行通信,獲取標(biāo)準(zhǔn)的輸入、標(biāo)準(zhǔn)輸出、標(biāo)準(zhǔn)錯誤以及返回碼等。
更多查看官網(wǎng):https://docs.python.org/2/library/subprocess.html?highlight=subprocess#frequently-used-arguments
import subprocess
'''
sh-3.2# ls /Users/nick/Desktop |grep txt$
mysql.txt
tt.txt
事物.txt
'''
res1 = subprocess.Popen('ls /Users/jieli/Desktop',shell=True, stdout=subprocess.PIPE)
res = subprocess.Popen('grep txt$', shell=True,stdin=res1.stdout, stdout=subprocess.PIPE)
print(res.stdout.read().decode('utf-8'))
# 下面這段等同于上面,但是上面的優(yōu)勢在于,一個數(shù)據(jù)流可以和另外一個數(shù)據(jù)流交互,可以通過爬蟲得到結(jié)果然后交給grep。
res1 = subprocess.Popen('ls /Users/jieli/Desktop |grep txt$',shell=True, stdout=subprocess.PIPE)
print(res1.stdout.read().decode('utf-8'))
# windows下:
# dir | findstr 'test*'
# dir | findstr 'txt$'
res1 = subprocess.Popen(r'dir “C:\Users\Administrator\PycharmProjects\test\函數(shù)備課”', shell=True, stdout=subprocess.PIPE)
res = subprocess.Popen('findstr test*', shell=True, stdin=res1.stdout, stdout=subprocess.PIPE)
# subprocess使用當(dāng)前系統(tǒng)默認(rèn)編碼,得到結(jié)果為bytes類型,在windows下需要用gbk解碼
print(res.stdout.read().decode('gbk') )到此這篇關(guān)于Python使用shutil操作文件、subprocess運(yùn)行子程序的文章就介紹到這了。希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Python利用shutil模塊實現(xiàn)文件夾的復(fù)制刪除與裁剪
- Python利用shutil模塊實現(xiàn)文件的裁剪與壓縮
- Python中shutil模塊的使用詳解
- Python 中 Shutil 模塊詳情
- python shutil操作文件實例講解
- 解決python subprocess參數(shù)shell=True踩到的坑
- Python中判斷subprocess調(diào)起的shell命令是否結(jié)束
- python subprocess pipe 實時輸出日志的操作
- 使用python執(zhí)行shell腳本 并動態(tài)傳參 及subprocess的使用詳解
- python中的subprocess.Popen()使用詳解
相關(guān)文章
使用Python的networkx繪制精美網(wǎng)絡(luò)圖教程
今天小編就為大家分享一篇使用Python的networkx繪制精美網(wǎng)絡(luò)圖教程,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-11-11
Python保留指定位數(shù)小數(shù)的5種方法總結(jié)
很多小伙伴在學(xué)習(xí)python的時候可能會遇到對數(shù)據(jù)進(jìn)行格式化輸出的需求,其中最常見的需求為保留幾位小數(shù),這篇文章主要給大家介紹了關(guān)于Python保留指定位數(shù)小數(shù)的5種方法,需要的朋友可以參考下2023-08-08
python調(diào)用攝像頭拍攝數(shù)據(jù)集
這篇文章主要為大家詳細(xì)介紹了Python調(diào)用攝像頭拍攝數(shù)據(jù)集,具有一定的參考價值,感興趣的小伙伴們可以參考一下2019-06-06
Python中函數(shù)的參數(shù)定義和可變參數(shù)用法實例分析
這篇文章主要介紹了Python中函數(shù)的參數(shù)定義和可變參數(shù)用法,以實例形式較為詳細(xì)的分析了Python中參數(shù)定義與可變參數(shù)的具體使用方法,需要的朋友可以參考下2015-06-06
使用 Django Highcharts 實現(xiàn)數(shù)據(jù)可視化過程解析
這篇文章主要介紹了使用 Django Highcharts 實現(xiàn)數(shù)據(jù)可視化過程解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2019-07-07
Python常見內(nèi)置高階函數(shù)即高階函數(shù)用法
這篇文章主要介紹了Python的三種高階函數(shù)map、filter、reduce,高階函數(shù)就是一個函數(shù)可以作為參數(shù)傳給另外一個函數(shù),或者一個函數(shù)的返回值為另外一個函數(shù)(若返回值為該函數(shù)本身,則為遞歸),滿足其一則為高階函數(shù),具體內(nèi)容,需要的朋友可以參考下面文章的介紹2021-12-12
淺談anaconda python 版本對應(yīng)關(guān)系
這篇文章主要介紹了淺談anaconda python 版本對應(yīng)關(guān)系,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-10-10

