发布时间:2022-05-25 09:33:21 人气:277 作者:多测师
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相关知识,请关注多测师。https://www.e70w.com/xwzx/