詳解Python shutil模組

2022-10-04 17:57:12 字數 1714 閱讀 5547

import shutil

高階的檔案,資料夾,壓縮包的處理模組,也主要用於檔案的拷貝

shutil.copyfileobj(fsrc,fdst[,length]):將檔案的內容拷貝到另乙個檔案(可以指定length長度進行拷貝)

import shutil

shutil.copyfilewww.cppcns.comobj(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模組

本文位址: /jiaoben/python/254490.html

模運算 詳解

模運算是大數運算中的常用操作。如果乙個數太大,無法直接輸出,或者不需要直接輸出,可以把它取模後縮小數值在輸出。定義模運算為 a 除以 m 的餘數,記為 a mod m a m 取模的結果滿足 0 a mod m m 1,題目用給定的 m 限制計算結果的範圍。例如 m 10,就是取計算結果的個位數,參...

Python shutil模組用法

1.shutil.copyfile oldfile,newfile 複製檔案1到檔案2中,如txt檔案。注意 若檔案2不存在,則直接建立檔案2,且檔案2中內容和檔案1內容相同。若檔案2存在,則檔案2中原有內容會被清除掉。語法 shutil.copyfile oldfile,newfile impor...

shutil模組 python shutil模組

shutil.copyfile src,dst 從源src複製到dst中去。當然前提是目標位址是具備可寫許可權。丟擲的異常資訊為ioexception.如果當前的dst已存在的話就會被覆蓋掉 shutil.move src,dst 移動檔案或重新命名 shutil.copymode src,dst ...