os shutil 的建立刪除移動複製

2021-10-08 19:12:25 字數 2162 閱讀 1905

os 建立

os.mknod(

) 建立檔案

os.mkdir(

) 建立資料夾(目錄)

os 刪除

os.remove(

)刪除檔案

os.rmdir(

) 刪除資料夾(目錄)

os.rename 對檔案,資料夾重新命名

os.rename(破名,新名)
os.makedirs 遞迴建立資料夾

os.makedirs(

"a/b/c/d/e"

)

os.removedirs 遞迴刪除資料夾

os.removedirs(

"a/b/c/d/e"

)

shutil.copyfileobj 單純的複製檔案的內容 (開啟檔案複製內容)

copyfileobj(fsrc, fdst[

, length=16*

1024

]) 複製檔案 (length的單位是字元(表達一次讀多少字元)

)import shutil

with

open

("test01.txt"

,"r+"

,encoding=

'utf-8'

)as fp ,

open

("test7.txt"

,"w"

,encoding=

"utf-8"

)as fp1:

shutil.copyfileobj(fp,fp1)

# 把檔案test01複製到test7

shutil.copyfile 單純的僅複製檔案內容,底層呼叫了copyfileobj

shutil.copyfile(

"test7.txt"

,"w.txt"

)

shutil.copymode 單純的複製檔案許可權,不包括內容

shutil.copymode(

"test01.txt"

,"test7.txt"

)

shutil.copystat 複製所有資訊,包括許可權,組 時間,修改時間等,不包括內容

shutil.copystat(

"test01.txt"

,"test7.txt"

)

shutil.copy 複製檔案許可權和內容

copy(src,dst)

#複製檔案許可權和內容 ***

shutil.copy(

"test01.txt"

,"test7.txt"

)

shutil.copy2 複製所有資訊,包括內容

copy2(src,dst)

#複製檔案許可權和內容,還包括許可權,組,使用者,時間等 ***

shutil.copy2(

"test01.txt"

,"test7.txt"

)

shutil.copytree 遞迴拷貝 (資料夾) 不包括 組,時間等

copytree(src,dst)

#拷貝資料夾裡所有內容(遞迴拷貝)

shutil.copytree(

"lianxi100"

,"lianxi101"

)+

shutil.rmtree 遞迴刪除

rmtree(path)

#刪除當前資料夾及其中所有內容(遞迴刪除)

shutil.rmtree(

"lianxi101"

)

shutil.move 移動檔案或資料夾

shutil.move(

"lianxi1.py"

,".."

)# 移動到上一層級

shutil.move(

"lianxi100"

,"../lianxi1000"

)# 移動並改名;

Directory的建立,刪除,移動

1.目錄 var path e april 2.建立目錄 if directory.exists path 3.刪除目錄 if directory.exists path 當path為空時,刪除成功。當path不為空時,會報異常 system.io.ioexception 目錄不是空的 給delet...

建立 移動和刪除的功能

l 基本操作 建立 移動和刪除的功能 l 命名空間system.io l directory常用方法directory提供靜態方法,與之對應的directoryinfo可以提供例項方法 l createdirectory move delete exist 方法的應用 using system.io...

Linux檔案建立 刪除 拷貝 移動

建立檔案命令 touch 建立的是沒有任何內容的空檔案。如在當前目錄建立沒有任何內容的空檔案 touch empty 注意事項 如果建立的檔案已經存在的話,touch檔案將會改變這個檔案的時間撮屬性,也即檔案的最後修改時間屬性 拷貝檔案 用法 cp 選項 要拷貝的檔案 檔案列表 目的檔名 目的目錄 ...