怎樣使用 Python 來判斷一個路徑是否存在判斷一個路徑是文件還是目錄
判斷一個路徑是否存在
可以判斷一個文件或目錄(文件夾)是否存在
import os.path
os.path.exists(path);
- 1
- 2
- 1
- 2
判斷一個文件是否存在
import os.path
os.path.isfile(path);
- 1
- 2
- 1
- 2
判斷一個目錄(文件夾)是否存在
import os.path
os.path.isdir(path);
- 1
- 2
- 1
- 2
判斷一個路徑是文件還是目錄(文件夾)
- 方法一
import os.path os.path.isdir(path); # 返回 True 表示是目錄(文件夾)
- 方法二
import os.path os.path.isfile(path); # 返回 True 表示是文件