1、将str类型转换为path,使用pathlib中的模块进行检查 from pathlib import Path rott='/tmp/f' root=Path(rott) #判断文件是否存在: if root.is_file(): #判断目录是否存在 if root.is_dir(): #判断文件或者目录是否存在: if root.exists(): 2、使用os.path.exists()检查 import os.path os.path.isfile(fname) #用于检查文件是否存在