您的位置: 首页 > IT文章 > 如何判断一个文件是否存在 如何判断一个文件是否存在 分类: IT文章 • 2022-05-28 14:45:17 1.使用os模块 import os os.path.exists(test_file.txt) #True os.path.exists(no_exist_file.txt) #False 2.使用Try语句 # try: # f =open('1.txt','r',encoding='utf-8') # f.close() # except IOError: # print("File is not accessible.") # else: # print('存在')