Python 中 Shutil 模塊詳情
一、什么是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ù)制軟連接,False
不復(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è)置此值為T(mén)rue。默認(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)文件,它類(lèi)似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
:壓縮包種類(lèi),“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é)
到此這篇關(guān)于 Python 中 Shutil 模塊詳情的文章就介紹到這了,更多相關(guān) Python 中 Shutil 模塊內(nèi)容請(qǐng)搜索本站以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持本站!
版權(quán)聲明:本站文章來(lái)源標(biāo)注為YINGSOO的內(nèi)容版權(quán)均為本站所有,歡迎引用、轉(zhuǎn)載,請(qǐng)保持原文完整并注明來(lái)源及原文鏈接。禁止復(fù)制或仿造本網(wǎng)站,禁止在非www.sddonglingsh.com所屬的服務(wù)器上建立鏡像,否則將依法追究法律責(zé)任。本站部分內(nèi)容來(lái)源于網(wǎng)友推薦、互聯(lián)網(wǎng)收集整理而來(lái),僅供學(xué)習(xí)參考,不代表本站立場(chǎng),如有內(nèi)容涉嫌侵權(quán),請(qǐng)聯(lián)系alex-e#qq.com處理。