总是忘记需要现查,记录一下。
os.path.exists(path_str):检查路径字符串path_str指示的路径是否存在。os.getcwd():获取当前工作目录。os.makedirs(path_str):根据path_str创建目录。os.path.dirname(file_path_str):获得文件所在路径。
实例:在写文件/home/baldlee/data/x1/settings.json前,补全缺失的目录。(直接调用open会提示路径不存在错误)
1 | import os |