Python os.path() 模块os.path.exists()

方法说明

os.path.exists(path)	
如果路径 path 存在,返回 True;如果路径 path 不存在,返回 False


举例

import os
 
#判断文件夹是否存在
dir = os.path.exists('C:\\Users')
print('dir:', dir)
 
#判断文件是否存在
file = os.path.exists('C:\\Users\\csdn.txt')
print('file:', file)

运行结果:

dir:True
file:False

你可能感兴趣的:(Python入门)