怎样使用 Python 来判断一个路径是否存在判断一个路径是文件还是目录
判断一个路径是否存在
可以判断一个文件或目录(文件夹)是否存在
import os.path
os.path.exists(path);
判断一个文件是否存在
import os.path
os.path.isfile(path);
判断一个目录(文件夹)是否存在
import os.path
os.path.isdir(path);
判断一个路径是文件还是目录(文件夹)
- 方法一
import os.path
os.path.isdir(path);
# 返回 True 表示是目录(文件夹)
- 方法二
import os.path
os.path.isfile(path);
# 返回 True 表示是文件
经常会遇到在目录下过滤特定后缀名的文件的需求。自己总结下面两个方法:
第一种方法、比较常规:代码如下
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
|
执行结果如下:
1 |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
|
执行结果如下: