Python shutil模塊用法實(shí)例分析
本文實(shí)例講述了Python shutil模塊用法。分享給大家供大家參考,具體如下:
shutil模塊
主要作用與拷貝文件用的。
1.shutil.copyfileobj(文件1,文件2):將文件1的數(shù)據(jù)覆蓋copy給文件2。
import shutil
f1 = open("1.txt",encoding="utf-8")
f2 = open("2.txt","w",encoding="utf-8")
shutil.copyfileobj(f1,f2)
2.shutil.copyfile(文件1,文件2):不用打開文件,直接用文件名進(jìn)行覆蓋copy。
import shutil
shutil.copyfile("1.txt","3.txt")
3.shutil.copymode(文件1,文件2):之拷貝權(quán)限,內(nèi)容組,用戶,均不變。
def copymode(src,dst):
"""copy mode bits from src to dst"""
if hasattr(os,'chmod'):
st = os.stat(stc)
mode = stat.S_IMODE(st.st_mode)
os.chmod(dst,mode)
4.shutil.copystat(文件1,文件):只拷貝了權(quán)限。
def copystat(src,dst):
"""將所有的狀態(tài)信息(模式位、時(shí)間、時(shí)間、標(biāo)志)從src復(fù)制到dst"""
st = os.stat(src)
mode = stat.S_IMODE(st.st_mode)
if hasattr(os, 'utime'):
os.utime(dst,(st.st_atime,st.st_mtime))
if hasattr(os, 'chmod')
os.chmod(dst,mode)
if hasattr(os, 'chflags') and hasattr(st,'st_flags'):
try:
os.chflags(dst, st.st_flags)
except OSError,why:
for err in 'EOPNOTSUPP', 'ENOTSUP':
if hasattr(errno,err) and why.errno == getattr(errno, err):
break
else:
raise
5.shutil.copy(文件1,文件2):拷貝文件和權(quán)限都進(jìn)行copy。
def copy(src,dst):
"""copy data and mode bits ("cp src dst")
The destination may be a directory.
"""
if os.path.isdir(dst):
dst = os.path.join(dst,os.path.basename(src))
copyfile(src,dst)
copymode(src,dst)
6.shutil.copy2(文件1,文件2):拷貝了文件和狀態(tài)信息。
7.shutil.copytree(源目錄,目標(biāo)目錄):可以遞歸copy多個(gè)目錄到指定目錄下。
- shutil.ignore_patterns(*patterns)
- shutil.copytree(src, dst, symlinks=False, ignore=None)
遞歸的去拷貝文件
例如:copytree(source, destination, ignore=ignore_patterns('*.pyc', 'tmp*'))
8.shutil.rmtree(目標(biāo)目錄):可以遞歸刪除目錄下的目錄及文件。
9.shutil.move(源文件,指定路徑):遞歸移動(dòng)一個(gè)文件。
10.shutil.make_archive():可以壓縮,打包文件。
import shutil
shutil.make_archive("shutil_archive_test","zip","D:\新建文件夾 (2)")
11.shutil.make_archive(base_name, format,...)
創(chuàng)建壓縮包并返回文件路徑,例如:zip、tar
- base_name: 壓縮包的文件名,也可以是壓縮包的路徑。只是文件名時(shí),則保存至當(dāng)前目錄,否則保存至指定路徑,
如:www =>保存至當(dāng)前路徑
如:/Users/wupeiqi/www =>保存至/Users/wupeiqi/ - 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ì)象
#將 /Users/wupeiqi/Downloads/test 下的文件打包放置當(dāng)前程序目錄
import shutil
ret = shutil.make_archive("wwwwwwwwww", 'gztar', root_dir='/Users/wupeiqi/Downloads/test')
#將 /Users/wupeiqi/Downloads/test 下的文件打包放置 /Users/wupeiqi/目錄
import shutil
ret = shutil.make_archive("/Users/wupeiqi/wwwwwwwwww", 'gztar', root_dir='/Users/wupeiqi/Downloads/test')
shutil 對(duì)壓縮包的處理是調(diào)用 ZipFile 和 TarFile 兩個(gè)模塊來(lái)進(jìn)行的,詳細(xì):
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()
z.close()
tarfile 壓縮解壓
import tarfile
# 壓縮
tar = tarfile.open('your.tar','w')
tar.add('/Users/wupeiqi/PycharmProjects/bbs2.zip', arcname='bbs2.zip')
tar.add('/Users/wupeiqi/PycharmProjects/cmdb.zip', arcname='cmdb.zip')
tar.close()
# 解壓
tar = tarfile.open('your.tar','r')
tar.extractall() # 可設(shè)置解壓地址
tar.close()
第二種方法:
import zipfile
z = zipfile.ZipFile("day5.zip","w")
z.write("a")
解壓:
z.extractall("a")
更多關(guān)于Python相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Python文件與目錄操作技巧匯總》、《Python文本文件操作技巧匯總》、《Python數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Python函數(shù)使用技巧總結(jié)》、《Python字符串操作技巧匯總》及《Python入門與進(jìn)階經(jīng)典教程》
希望本文所述對(duì)大家Python程序設(shè)計(jì)有所幫助。
相關(guān)文章
python 虛擬環(huán)境調(diào)用allure報(bào)錯(cuò):FileNotFoundError: [WinError
python代碼調(diào)用命令行 allure命令報(bào)錯(cuò),提示找不到allure這個(gè)命令,本文就詳細(xì)的介紹了具體的解決方法,具有一定的參考價(jià)值,感興趣的可以了解一下2023-09-09
python實(shí)戰(zhàn)之利用pygame實(shí)現(xiàn)貪吃蛇游戲(一)
這篇文章主要介紹了python實(shí)戰(zhàn)之利用pygame實(shí)現(xiàn)貪吃蛇游戲,文中有非常詳細(xì)的代碼示例,對(duì)正在學(xué)習(xí)python的小伙伴們有很好的幫助喲,需要的朋友可以參考下2021-05-05
Python數(shù)據(jù)序列化技術(shù)總結(jié)
在現(xiàn)代軟件開發(fā)中,數(shù)據(jù)序列化是一個(gè)關(guān)鍵環(huán)節(jié),它允許我們將復(fù)雜的數(shù)據(jù)結(jié)構(gòu)轉(zhuǎn)換為可存儲(chǔ)或可傳輸?shù)母袷?,Python提供了多種數(shù)據(jù)序列化技術(shù),每種技術(shù)都有其獨(dú)特的性能優(yōu)勢(shì)和適用場(chǎng)景,本文將詳細(xì)介紹幾種強(qiáng)大的Python數(shù)據(jù)序列化技術(shù),需要的朋友可以參考下2025-03-03
簡(jiǎn)單談?wù)凱ython中的json與pickle
下面小編就為大家?guī)?lái)一篇簡(jiǎn)單談?wù)凱ython中的json與pickle。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-07-07
Python類的動(dòng)態(tài)修改的實(shí)例方法
這篇文章主要介紹了Python類的動(dòng)態(tài)修改的實(shí)例方法的相關(guān)資料,需要的朋友可以參考下2017-03-03
Python運(yùn)行時(shí)修改業(yè)務(wù)SQL代碼
這篇文章主要介紹了Python運(yùn)行時(shí)修改業(yè)務(wù)SQL代碼,文章圍繞主題展開詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,需要的小伙伴可以參考一下2022-06-06

