python中shutil模組的使用

2022-04-10 17:33:15 字數 1041 閱讀 5388

可以操作許可權的處理檔案模組:shutil

# 基於路徑的檔案複製

import shutil

shutil.copyfile("oldfile_path","newfile_path")

例:

# shutil.copyfile("a.txt","b.txt")
#基於檔案流的檔案複製

with open("oldfile_path","rb") as f, open("newfile_path","wb") as s:

shutil.copyfileobj(f,s)

例:

# with open("a.txt","rb") as f,open("b.txt","wb") as s:

# shutil.copyfileobj(f,s)

# 遞迴刪除目標目錄,無條件刪除

shutil.rmtree("target_file")  

例:

# shutil.rmtree(r'c:\users\dell\pycharmprojects\untitled1\four week\mudule04\text')
#檔案移動,可重新命名,會刪除原檔案,新路徑需指定檔名

shutil.remove("old_path","new_path")

例:

# shutil.move("b.txt","text/b.txt")
# 資料夾壓縮,不可壓縮檔案

shutil.make_achive("資料夾路徑","格式format","壓縮後的路徑及包名")

例:

# shutil.make_archive("text","zip",r"c:\users\dell\pycharmprojects\untitled1\four week\mudule04")
# 資料夾解壓

shutil.unpack_archive("壓縮包路徑","解壓後的路徑及建立的資料夾名","格式format")

例:

# shutil.unpack_archive("text.zip","text","zip")

python中的shutil模組

shutil模組簡介 是python內建的高階的檔案 資料夾 壓縮包處理模組 將src複製到dst中去,dst一定要具有讀寫許可權,如果dst已經存在會被覆蓋,src和dst必須是檔案,不可以是目錄。移動檔案 目錄。或者檔案 目錄重新命名,如果dst存在,則不可覆蓋。複製乙個檔案到乙個檔案或乙個目錄...

python中的shutil模組

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

python中的shutil模組

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