這篇文章主要介紹了Python_shutil模塊功能,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨着小編來一起學習學習吧
import shutil
高級的文件,文件夾,壓縮包的處理模塊,也主要用於文件的拷貝
shutil.copyfileobj(fsrc,fdst[,length]):
將文件的內容拷貝到另一個文件(可以指定length長度進行拷貝)
import shutil shutil.copyfileobj(open('old.txt','r'),open('new.txt','w'))
shutil.copyfile(src,dst):
拷貝文件
import shutil shutil.copyfile('f1.log','f2.log')
shutil.copymode(src,dst):
僅拷貝權限,內容、組、用戶均不變
import shutil shutil.copymode('f1.log', 'f2.log')
shutil.copystat(src,dst):
拷貝狀態的信息,包括:mode bits,atime,mtime,flags
import shutil shutil.copystat('f1.log', 'f2.log')
shutil.copy(src,dst):
拷貝文件和權限
import shutil shutil.copy('f1.log', 'f2.log')
shutil.copy2(src,dst):
拷貝文件和狀態信息
import shutil shutil.copy2('f1.log', 'f2.log')
shutil.copytree(src,det,symlinks=False,ignore=None):
遞歸的去拷貝文件
import shutil shutil.copytree('folder1', 'folder2', ignore=shutil.ignore_patterns('*.pyc', 'tmp*'))
shutil.rmtree(path[,ignore_errors[,onerror]]):
遞歸的去刪除文件
import shutil shutil.rmtree('folder1')
shutil.move(src,dst):
遞歸的去移動文件(重命名)
import shutil shutil.move('folder1', 'folder3')
shutil.make_archive(base_name, format,...): 創建壓縮包並返回文件路徑,例如:zip、tar
base_name: 壓縮包的文件名,也可以是壓縮包的路徑。只是文件名時,則保存至當前目錄,否則保存至指定路徑(例:Presley=>保存至當前路徑,/User/Presley =>保存至/Users/路徑下)
format: 壓縮包種類,“zip”, “tar”, “bztar”,“gztar”
root_dir: 要壓縮的文件夾路徑(默認當前目錄)
owner: 用戶,默認當前用戶
group: 組,默認當前組
import shutil z = shutil.make_archive('presly', 'gztar', root_dir='D:\軟件下載')
shutil對壓縮包的處理,也可調用zipfile或tarfile模塊進行壓縮
以上所述是小編給大家介紹的Python_shutil模塊詳解整合,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回覆大家的。在此也非常感謝大家對神馬文庫網站的支持!