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

2020-11-17 0 521

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判断文件或文件夹是否存在

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

到此这篇关于详解python os.path.exists判断文件或文件夹是否存在的文章就介绍到这了,更多相关python os.path.exists判断文件存在内容请搜索自学编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持自学编程网!

遇见资源网 Linux 详解python os.path.exists判断文件或文件夹是否存在 http://www.ox520.com/29860.html

常见问题

相关文章

发表评论
暂无评论
官方客服团队

为您解决烦忧 - 24小时在线 专业服务