python判断文件夹下有没有存在文件的方法

小编给大家分享一下python判断文件夹下有没有存在文件的方法,希望大家阅读完这篇文章后大所收获,下面让我们一起去探讨方法吧!

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判断文件夹下有没有存在文件的方法有了一定的了解,想了解更多相关知识,欢迎关注行业资讯频道,感谢各位的阅读!