详解python os.path.exists判断文件或文件夹是否存在

os即operating system(操作系统),Python 的 os 模块封装了常见的文件和目录操作。

os.path模块主要用于文件的属性获取,exists是“存在”的意思,所以顾名思义,os.path.exists()就是判断括号里的文件是否存在的意思,括号内的可以是文件路径。

举个栗子:

import os

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

#判断文件是否存在
file = os.path.exists(\'C:\\\\Users\\\\Desktop\\\\雍正王朝.txt\')
print(\'file:\', file)

运行结果

详解python os.path.exists判断文件或文件夹是否存在

显示该文件及文件夹都存在

© 版权声明
THE END
喜欢就支持一下吧
点赞0 分享
评论 抢沙发

请登录后发表评论

    暂无评论内容