檔案遍歷及資料夾的刪除

2022-07-04 17:48:10 字數 1103 閱讀 8398

檔案遍歷有兩種方法,一種使用glob函式以陣列的形式取到指定目錄下的檔案;另一種是用opendir與closedir通過開啟關閉資源的方法用readdir

函式取到所有檔案,但是通過這種方法會取到「.」與「..」兩個檔案,這兩個會返回上級目錄,遍歷時應跳過這兩項。

1.利用glob函式遍歷檔案,並用filesize()函式取到制定目錄下檔案的總大小:

function shuliang($filename)

else

} return $size;

}echo shuliang("路徑");

?>

2.是用opendir與closedir通過開啟關閉資源的方法用readdir函式取到所有檔案,同樣取檔案總大小:

function fsize($fname)

else

} }

closedir($dir); //關閉資源

return $size;

}echo fsize("路徑");

結果正確。

資料夾刪除

沒有直接刪除資料夾的函式,但是有刪除檔案的函式unlink()與刪除空資料夾的函式rmdir(),同樣運用遞迴遍歷到所有檔案都刪除掉,最後刪除這個空資料夾:

function shanchu($fname)

else

}} closedir($dir);

//刪除資料夾

rmdir($fname);

}shanchu("路徑");

python 遍歷資料夾及檔案

coding cp936 import os import os.path import configparser rootdir r d project 指明被遍歷的資料夾 pathlist list pathlist update list pathdict dict pathlist dict...

C 遍歷資料夾及檔案

背景 想自己實現乙個網盤系統,於是需要用到遍歷檔案 夾 操作。1.如何獲取指定目錄包含的檔案和子目錄 1 directoryinfo.getfiles 獲取目錄中 不包含子目錄 的檔案,返回型別為fileinfo,支援萬用字元查詢 2 directoryinfo.getdirectories 獲取目...

java刪除檔案及資料夾

刪除指定資料夾下所有檔案 param path 資料夾完整絕對路徑 param path return public static boolean delallfile string path if file.isdirectory string templist file.list file te...