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

2023-12-13 10:57:26网络知识匿名

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

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

importos

os.path.exists(test_file.txt)

#True

os.path.exists(no_exist_file.txt)

#False

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

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

importos

dirct='/home/workespace/notebook/'foriinos.listdir(dirct):

fulldirct=os.path.join(dirct,i)

ifos.path.isfile(fulldirct):#入参需要是绝对路径

print(i)

以上内容为大家介绍了python怎么判断文件夹下是否存在文件?希望对大家有所帮助,如果想要了解更多Python相关知识,请关注IT培训机构:瀚银百科。

发表评论: