os判断文件是否存在不存在创建

法一

import os
if not os.path.exists(dir):
    os.mkdirs(dir)

法二

exist_ok=False 默认,如果目录已存在抛出异常,不存在则创建。
True,若不存在则创建,若存在不会抛异常,继续让它存在。
exist_ok (optional) : A default value False is used for this parameter. If the target directory already exists an OSError is raised if its value is False otherwise not. For value True leaves directory unaltered.

os.makedirs(dir, exist_ok=True)

你可能感兴趣的:(python编程,python)