python判断文件(文件夹)是否存在,不存在创建

tech2022-07-13  153

在Ubuntus均可用

在Windows上只有创建文件夹代码可用,创建文件是不可用的. 判断是否存在文件夹.不存在创建.

判断是否存在文件,不存在创建.

import os cur_path = os.path.dirname(os.path.realpath(__file__)) # log_path是存放日志的路径 LOG_PATH = os.path.join(os.path.dirname(cur_path), 'logs') if not os.path.exists(LOG_PATH): os.mkdir(LOG_PATH) # 如果不存在这个logs文件夹,就自动创建一个 log_file = os.path.join(LOG_PATH, 'asm.log') if not os.path.exists(log_file): os.system(r"touch {}".format(log_file)) # 如果不存在这个文件,就自动创建一个
最新回复(0)