在Python中,`exists`通常与`os.path`模块一起使用,用于判断指定的路径是否存在。如果路径存在,无论是文件还是目录,`exists`函数都会返回`True`;如果路径不存在,则返回`False`。
下面是一个使用`os.path.exists`函数的简单示例:
import os
判断文件夹是否存在
dir_path = 'C:\\Users\\Desktop'
dir_exists = os.path.exists(dir_path)
print('Directory exists:', dir_exists)
判断文件是否存在
file_path = 'C:\\Users\\Desktop\\example.txt'
file_exists = os.path.exists(file_path)
print('File exists:', file_exists)
需要注意的是,`exists`函数只关心路径是否存在,而不关心路径指向的是文件还是目录