ITPub博客

首页 > IT职业 > IT职场 > python怎么判断文件夹下是否存在文件?

python怎么判断文件夹下是否存在文件?

IT职场 作者:britain 时间:2021-09-11 10:14:59 0 删除 编辑

python判断文件夹下是否存在文件的方法:

1、使用os.path.exists()方法判断文件是否存在

import os
os.path.exists(test_file.txt)
#True
os.path.exists(no_exist_file.txt)
#False

2、使用os.path.isfile()方法判断文件是否存在

os.path.isfile(path):判断路径是否为文件    

import os
dirct = '/home/workespace/notebook/'for i in os.listdir(dirct):
    fulldirct = os.path.join(dirct, i)
    if os.path.isfile(fulldirct): #入参需要是绝对路径
        print(i)

更多Python知识请关注Python自学网

来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/2317/viewspace-2833333/ ,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论

注册时间:2019-04-16

  • 博文量
    72
  • 访问量
    628