一篇文章帶你搞定 Python 中 Shutil 模塊
Hey,大家好呀,我是Go進(jìn)階者。
一、什么是shutil
shutil可以簡(jiǎn)單地理解為sh + util,shell工具的意思。shutil模塊是對(duì)os模塊的補(bǔ)充,主要針對(duì)文件的拷貝、刪除、移動(dòng)、壓縮和解壓操作。
二、shutil模塊的主要方法
1. shutil.copyfileobj(fsrc, fdst[, length=16*1024])
copy文件內(nèi)容到另一個(gè)文件,可以copy指定大小的內(nèi)容。這個(gè)方法是shutil模塊中其它拷貝方法的基礎(chǔ),其它方法在本質(zhì)上都是調(diào)用這個(gè)方法。
讓我們看一下它的源碼:
- def copyfileobj(fsrc, fdst, length=16*1024):
 - while 1:
 - buf = fsrc.read(length)
 - if not buf:
 - break
 - fdst.write(buf)
 
代碼很簡(jiǎn)單,一看就懂。但是要注意,其中的fsrc,fdst都是使用open()方法打開(kāi)后的文件對(duì)象。
- import shutil
 - s =open('fsrc.txt','r')
 - d=open('fdst.txt','w')
 - shutil.copyfileobj(s,d,length=16*1024)
 
2. shutil.copyfile(src, dst)
拷貝文件
- shutil.copyfile('f1.log', 'f2.log') #目標(biāo)文件無(wú)需存在
 
3. shutil.copymode(src, dst)
僅拷貝權(quán)限。內(nèi)容、組、用戶均不變
- shutil.copymode('f1.log', 'f2.log') #目標(biāo)文件必須存在
 
4. shutil.copystat(src, dst)
僅拷貝狀態(tài)的信息,包括:mode bits, atime, mtime, flags
- shutil.copystat('f1.log', 'f2.log') #目標(biāo)文件必須存在
 
5. shutil.copy(src, dst)
拷貝文件和權(quán)限
- import shutil
 - shutil.copy('f1.log', 'f2.log')
 
6. shutil.copy2(src, dst)
拷貝文件和狀態(tài)信息
- import shutil
 - shutil.copy2('f1.log', 'f2.log')
 
7. shutil.copytree(src, dst, symlinks=False, ignore=None)
遞歸的去拷貝文件夾
- src:源文件夾
 - dst:復(fù)制至dst文件夾,該文件夾會(huì)自動(dòng)創(chuàng)建,需保證此文件夾不存在,否則將報(bào)錯(cuò)
 - symlinks:是否復(fù)制軟連接,True復(fù)制軟連接,F(xiàn)alse不復(fù)制,軟連接會(huì)被當(dāng)成文件復(fù)制過(guò)來(lái),默認(rèn)False
 - ignore:忽略模式,可傳入ignore_patterns()
 - copy_function:拷貝文件的方式,可以傳入一個(gè)可執(zhí)行的處理函數(shù),默認(rèn)為copy2,Python3新增參數(shù)
 - ignore_dangling_symlinks:sysmlinks設(shè)置為False時(shí),拷貝指向文件已刪除的軟連接時(shí),將會(huì)報(bào)錯(cuò),如果想消除這個(gè)異常,可以設(shè)置此值為True。默認(rèn)為False,Python3新增參數(shù)。
 
- import shutil,os
 - folder1 = os.path.join(os.getcwd(),"aaa")
 - # bbb與ccc文件夾都可以不存在,會(huì)自動(dòng)創(chuàng)建
 - folder2 = os.path.join(os.getcwd(),"bbb","ccc")
 - # 將"abc.txt","bcd.txt"忽略,不復(fù)制
 - shutil.copytree(folder1,folder2,ignore=shutil.ignore_patterns("abc.txt","bcd.txt"))
 
8. shutil.rmtree(path[, ignore_errors[, onerror]])
遞歸的去刪除文件
- import shutil
 - shutil.rmtree('folder1')
 
9. shutil.move(src, dst)
遞歸的去移動(dòng)文件,它類似mv命令,其實(shí)就是重命名。
- import shutil
 - shutil.move('folder1', 'folder3')
 
10.shutil.make_archive(base_name, format[, root_dir[, base_dir, verbose, dry_run, owner, group, logger])
創(chuàng)建壓縮包并返回文件路徑,例如:zip、tar
創(chuàng)建壓縮包并返回文件路徑,例如:zip、tar
- base_name:壓縮包的文件名,也可以是壓縮包的路徑。只是文件名時(shí),則保存至當(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對(duì)象
把當(dāng)前目錄下的文件壓縮生成copy.zip文件到當(dāng)前目錄下注意:此操作會(huì)出現(xiàn)遞歸拷貝壓縮導(dǎo)致文件損壞(當(dāng)前目錄下的copy.zip中會(huì)有copy.zip)
- import shutil
 - shutil.make_archives('D:\copy3\copy','zip',base_dir='D:\copy2\\測(cè)試.txt')
 
把D:\copy2\測(cè)試.txt文件壓縮,在D:\copy3\路徑下生成copy.zip。
- import shutil
 - shutil.make_archives('copy','zip')
 
三、總結(jié)
本文主要介紹了Python中shutil模塊,對(duì)模塊中主要的方法進(jìn)行了詳細(xì)的介紹。對(duì)遇到的問(wèn)題進(jìn)行詳細(xì)的解答。最后使用Python編程語(yǔ)言,通過(guò)在實(shí)際開(kāi)發(fā)中的項(xiàng)目。方便大家對(duì)shutil模塊的認(rèn)知。希望對(duì)大家的學(xué)習(xí)有幫助。
本文轉(zhuǎn)載自微信公眾號(hào)「Go語(yǔ)言進(jìn)階學(xué)習(xí)」,可以通過(guò)以下二維碼關(guān)注。轉(zhuǎn)載本文請(qǐng)聯(lián)系Go語(yǔ)言進(jìn)階學(xué)習(xí)公眾號(hào)。




















 
 
 






 
 
 
 