Python Python如何知道文件是否存在?
2022-12-1 09:17
阅读:1574
使用os模块
import os os.path.exists(hh.txt) 返回:Trueos.path.exists(hh.txt) 返回:False
上面的代码是对一个文件进行判断,它还有判断一个文件夹是否存在,例:
import os os.path.exists(test_dir) 输出:Trueos.path.exists(no_exist_dir) 输出:False
在上段的代码中,我们可以知道在os模块中,不管是对文件还是文件夹进行判断,判断的方法都是一样的,
都使用了os.path.exits()的方法。
os.access判断文件是否存在,如果存在,返回true
os.access(filepathos.F_OK): xl = xlrd.open_workbook(filepath) table = xl.sheets()[] data = table.cell().value table.nrows
参考:https://www.bbsmax.com/A/RnJWppEo5q/
参考:https://blog.csdn.net/weixin_35682224/article/details/112367980
转载本文请联系原作者获取授权,同时请注明本文来自林清莹科学网博客。
链接地址:https://wap.sciencenet.cn/blog-3134052-1366007.html?mobile=1
收藏
当前推荐数:0
推荐到博客首页
网友评论0 条评论