python判断文件或文件夹是否存在
import os.path
dir_path = 'C:/Users/xxx/Desktop/'
file_name = '123.txt'
if os.path.isdir(dir_path): # 判断目录是否存在
print('%s 目录存在.' % dir_path)
os.chdir(dir_path) # 切换到该目录
if os.path.isfile_name(file_name): # 判断文件是否存在
print('%s 文件存在.' % file_name)
with open(dir_path + file_name, 'r', encoding='utf8') as file:
file_txt = file.readlines()
file.close() # 关闭文件