首页 > IT职业 > IT职场 > 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知识请关注Python自学网
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/2317/viewspace-2833333/ ,如需转载,请注明出处,否则将追究法律责任。