import shutil
高級的文件,文件夾,壓縮包的處理模塊,也主要用于文件的拷貝
shutil.copyfileobj(fsrc,fdst[,length]): 將文件的內容拷貝到另一個文件(可以指定length長度進行拷貝)
import shutilshutil.copyfileobj(open('old.txt','r'),open('new.txt','w')) shutil.copyfile(src,dst): 拷貝文件
import shutilshutil.copyfile('f1.log','f2.log') shutil.copymode(src,dst): 僅拷貝權限,內容、組、用戶均不變
import shutilshutil.copymode('f1.log', 'f2.log') shutil.copystat(src,dst): 拷貝狀態的信息,包括:mode bits,atime,mtime,flags
import shutilshutil.copystat('f1.log', 'f2.log') shutil.copy(src,dst): 拷貝文件和權限
import shutilshutil.copy('f1.log', 'f2.log') shutil.copy2(src,dst): 拷貝文件和狀態信息
import shutilshutil.copy2('f1.log', 'f2.log') shutil.copytree(src,det,symlinks=False,ignore=None): 遞歸的去拷貝文件
import shutilshutil.copytree('folder1', 'folder2', ignore=shutil.ignore_patterns('*.pyc', 'tmp*')) shutil.rmtree(path[,ignore_errors[,onerror]]): 遞歸的去刪除文件
import shutilshutil.rmtree('folder1') shutil.move(src,dst): 遞歸的去移動文件(重命名)
import shutilshutil.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 shutilz = shutil.make_archive('presly', 'gztar', root_dir='D:/軟件下載')shutil對壓縮包的處理,也可調用zipfile或tarfile模塊進行壓縮
以上所述是小編給大家介紹的Python_shutil模塊詳解整合,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對VEVB武林網網站的支持!
新聞熱點
疑難解答