ftw遍歷目錄樹

2021-05-31 22:29:53 字數 889 閱讀 6220

表頭檔案:#include

定義函式:int  ftw(const char *dir, int (*fn) (const *file, const struct stat *sb, int flag), int depth)

函式說明:ftw() 會從引數dir指定的 目錄開始,往下一層層地遞迴式遍歷子 目錄。ftw()會傳三個引數給fn(), 第乙個引數*file指向當時所在的 目錄路徑,第二個引數是*sb, 為stat結構指標,第三個引數為旗標,有下面幾種可能值:

ftw_f        一般檔案

ftw_d       目錄

ftw_dnr    不可讀取的 目錄,此 目錄以下將不被遍歷

ftw_sl       符號連線

ftw_ns       無法取得stat結構資料,有可能是 許可權問題

最後乙個引數depth代表ftw()在進行遍歷 目錄時同時開啟的檔案數。ftw()在遍歷時每一層 目錄至少需要乙個檔案描述詞,如果遍歷時用完了depth所給予的限制數目,整個遍歷將因不斷地關檔案和開檔案操作而顯得緩慢.

如果要結束ftw()的遍歷,fn()只需返回一非零值即可,此值同時也會是ftw()的返回值。否則ftw()會試著走完所有的 目錄,然後返回0.

返回值:遍歷中斷則返回fn()函式的返回值,全部遍歷則返回0,若有錯誤發生則返回-1.

附加說明:由於ftw()會動態配置 記憶體使用,請使用正常方式(fn函式返回非零值)來中斷遍歷,不要在fn函式中使用longjmp().

示例:

#include #include #include #include int fn(const char *file, const struct stat *sb, int flag)

int main()

遍歷目錄樹

cpath os.getcwd 如果目錄名字為中文 需要轉碼處理 upath unicode cpath,utf 8 for filename in os.listdir upath print filenameimport os for foldername,subfolders,filename...

nftw遍歷目錄樹

表頭檔案 include 定義函式 int nftw const char dir,int fn const char file,const struct stat sb,int flag,struct ftw s depth,int flags 函式說明 nftw 與ftw 很像,都是從引數dir...

python遍歷目錄樹

假定你希望對某個資料夾中的所有檔案改名,包括該資料夾中所有子資料夾中的所有檔案。也就是說,你希望遍歷目錄樹,處理遇到的每個檔案。寫程式完成這件事,可能需要一些技巧。好在,python 提供了乙個函式,替你處理這個過程。請看 c delicious 資料夾及其內容,如圖 9 1 所示。這裡有乙個例子程...