python之shutil模組的使用

2022-08-30 21:06:16 字數 4150 閱讀 7484

shutil模組

shutil模組是一種高階的檔案操作工具,其對檔案的複製與刪除操作非常強大,shutil 名字**於 shell utilities,該模組擁有許多檔案(夾)操作的功能,包括複製、移動、重新命名、刪除、壓縮,解壓等等;

常用功能:

shutil.copy():實現檔案複製功能,返回值是複製成功後的字串格式的檔案路徑;

shutil.copy2():在copy上的基礎上吧複製檔案最後訪問時間與修改時間也複製過來;

shutil.copyfileobj(fsrc, fdst[, length]):將檔案內容拷貝到另乙個檔案中shutil.copyfileobj(open('a.txt','r'),open('b.txt','w'))

>>> shutil.copy('

e:\\python\\a.txt

','e:\\win10

') #

如果後面跟目錄,直接在目錄下建立乙個新的檔案,與原始檔同名。

'e:\\win10\\a.txt

'>>> shutil.copy('

e:/python/a.txt

','e:/win10/a_1.txt

')#如果跟上檔名,等同於複製過來之後給檔案重新命名。

'e:/win10/a_1.txt

'

shutil.copytree():複製整個資料夾,裡面的檔案、子資料夾都會被複製過去,可做備份,

>>> shutil.copytree('

e:\\file

','e:\\file_1

')#此時

file_1檔案並不存在,如有此檔案則會報錯:(fileexistserror: [winerror 183] 當檔案已存在時,無法建立該檔案。: 'e:\\file_1')

'e:\\file_1

'

>>> shutil.copytree('e:\\file','e:\\file_1',ignore=shutil.ignore_patterns('*.pyc','tmp*'))#壓縮時忽略某些檔案

shutil.move(): 移動檔案或資料夾,返回值是移動後檔案的絕對路徑字串

>>> shutil.move('

e:\\file\\file2.txt

','e:\\file_1

')#檔案移動到其他目錄上,如果該目錄下存在同名檔案將會被重寫。

'e:\\file_1\\file2.txt

'>>> shutil.move('

e:\\file\\file2.txt

','e:\\file_1\\file_2.txt

')#檔案移動加重命名。

'e:\\file_1\\file_2.txt

'>>> shutil.move('

e:\\file\\file2.txt

','e:\\file_1\\file')

'e:\\file_1\\file

'>>> shutil.move('

e:\\file\\file1

','e:\\file_2\\

')#file1資料夾下的檔案移動到file_2資料夾下

'e:\\file_2\\

'

shutil.rmtree(path):刪除檔案,(謹慎使用

shutil.get_archive_formats():返回支援的格式列表;

>>>shutil.get_archive_formats()[('

bztar

', "

bzip2'ed tar-file

"), ('

gztar

', "

gzip'ed tar-file

"), ('

tar', '

uncompressed tar file

'), ('

xztar

', "

xz'ed tar-file

"), ('

zip', '

zip file

')]

shutil.make_archive(base_name,format, root_dir=none, base_dir=none, verbose=0,dry_run=0, owner=none, group=none, logger=none):檔案壓縮

----base_name: 壓縮包的檔名,也可以是壓縮包的路徑。只是檔名時,則儲存至當前目錄,否則儲存至指定路徑

----format(zip): 壓縮包種類,「zip」, 「tar」, 「bztar」,「gztar」

----root_dir

要壓縮的資料夾路徑(預設當前目錄)

----owner:

使用者,預設當前使用者

group 組,預設當前組

----logger用於記錄日誌,通常是logging.logger物件

>>> shutil.make_archive('

e:\\file_2\\name

','gztar

','e:\\file_1

')#將file_1目錄下的檔案以gztar格式壓縮放在file_2目錄下,名字為name

'e:\\file_2\\name.tar.gz

'

shutil.unpack_archive(filename[, extract_dir[,format]]):解壓

filename :歸檔檔案的全路徑

extract_dir :解壓歸檔的目標目錄名稱,如果沒有提供,則取當前工作目錄

format :歸檔格式:'zip', 'tar' 或 'gztar'中的一種。或者是通過register_unpack_format()註冊時的其他格式,如果未提供則會根據歸檔檔案的副檔名去查詢相應的解壓器,如果沒找到則會引發valueerror。

shutil對壓縮包的處理,通過呼叫zipfile 和tarfile兩個模組,zipfile是python裡用來做zip格式編碼的壓縮和解壓縮的,最重要兩個class----zipfile和zipinfo。tarfile模組用於讀寫tar歸檔檔案,它也可以同時實現壓縮功能。與zipfile模組相比,tarfile模組 可以直接將乙個目錄進行歸檔並壓縮。

python模組之shutil模組

高階的 檔案 資料夾 壓縮包 處理模組 shutil.copyfileobj fsrc,fdst length 將檔案內容拷貝到另乙個檔案中 import shutil shutil.copyfileobj open old.xml r open new.xml w shutil.copyfile ...

Python常用模組 shutil模組

import shutil copy 功能 複製檔案 格式 shutil.copy 檔案 目標位址 返回值 複製之後的路徑 copy2 功能 複製檔案,保留元資料 格式 shutil.copy2 檔案 目標位址 返回值 複製之後的路徑 copyfileobj 將乙個檔案的內容拷貝的另外乙個檔案當中 ...

模組之shutil模組模組詳解

shutil模組是高階的 檔案 資料夾 壓縮包 處理模組 shutil.copyfileobj fsrc,fdst length 將檔案內容拷貝到另乙個檔案中 shutil.copyfile src,dst 拷貝檔案 shutil.copymode src,dst 僅拷貝許可權。內容 組 使用者均不...