Python使用shutil模塊實(shí)現(xiàn)文件拷貝
主要作用與拷貝文件用的。
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')
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
python切片復(fù)制列表的知識(shí)點(diǎn)詳解
在本篇文章里小編給大家整理的是一篇關(guān)于python切片復(fù)制列表的知識(shí)點(diǎn)相關(guān)內(nèi)容,有興趣的朋友們可以跟著學(xué)習(xí)下。2021-10-10
python實(shí)現(xiàn)蒙特卡羅模擬法的實(shí)踐
?蒙特卡洛就是產(chǎn)生隨機(jī)變量,帶入模型算的結(jié)果,尋優(yōu)方面,本文主要介紹了python 蒙特卡羅模擬法實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-03-03
Python實(shí)現(xiàn)將16進(jìn)制字符串轉(zhuǎn)化為ascii字符的方法分析
這篇文章主要介紹了Python實(shí)現(xiàn)將16進(jìn)制字符串轉(zhuǎn)化為ascii字符的方法,結(jié)合實(shí)例形式分析了Python 16進(jìn)制字符串轉(zhuǎn)換為ascii字符的實(shí)現(xiàn)方法與相關(guān)注意事項(xiàng),需要的朋友可以參考下2017-07-07
pycharm配置pyqt5-tools開發(fā)環(huán)境的方法步驟
這篇文章主要介紹了pycharm配置pyqt5-tools開發(fā)環(huán)境的方法步驟,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2019-02-02
python實(shí)現(xiàn)簡單名片管理系統(tǒng)
這篇文章主要為大家詳細(xì)介紹了python實(shí)現(xiàn)簡單名片管理系統(tǒng),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-11-11
Python從ZabbixAPI獲取信息及實(shí)現(xiàn)Zabbix-API 監(jiān)控的方法
這篇文章主要介紹了Python從ZabbixAPI獲取信息及實(shí)現(xiàn)Zabbix-API 監(jiān)控的方法,需要的朋友可以參考下2018-09-09

