Linux下檔案及目錄是否存在

2021-07-31 13:06:29 字數 603 閱讀 5932

標頭檔案自己查,吧,或者我有空,再補上,話不多說,直接貼**

//判斷目錄是否存在,目錄存在返回0,不存在返回-1

int is_dir_exist(const

char * dir_path)

dir * dir = opendir(dir_path);

if(dir==null)

closedir(dir);

return0;}

//判斷檔案是否存在(是否能訪問到),路徑存在返回0,不存在返回-1

int is_access(const

char * path)

if(access(path ,f_ok)==0)

return -1;

}//如果能訪問,並且不是目錄的話,就是檔案,檔案存在返回0,不存在返回-1

int is_file_exist(const

char * file_path)

if(is_dir_exist(file_path)==-1)

return -1;

}

測試的**

void test()

查詢目錄或檔案是否存在

在專案中可能會遇見查詢某個路徑或者檔案是否存在的情況,如果不存在就新建。在看 的時候發現有人這樣實現 dir opendir g logdir if dir nullp else 對應檔案的話會這樣寫 dir fopen g logdir if dir nullp else 目的很簡單,就是查詢目錄...

shell判斷目錄 檔案是否存在

編寫指令碼 vim a.sh bin bash a ls grep zabbix b usr local src zabbix if d a then touch b echo b已建立 else echo a fi具體引數如下 根據自己需要選擇相對應的引數 e 判斷 a是否存在 d 判斷 a是否存...

Python 判斷檔案 目錄是否存在

python 操作檔案時,我們一般要先判斷指定的檔案或目錄是否存在,不然容易產生異常。例如我們可以使用 os 模組的 os.path.exists 方法來檢測檔案是否存在 import os.path os.path.isfile fname 如果你要確定他是檔案還是目錄,從 python 3.4 ...