python 判断目录和文件是否存在,若不存在即创建
2018年01月13日 13:52:19 仰望神的光 阅读数:15032
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u013247765/article/details/79050947
判断目录是否存在
import os
dirs = '/Users/joseph/work/python/'
if not os.path.exists(dirs):
os.makedirs(dirs)
判断文件是否存在
import os
filename = '/Users/joseph/work/python/poem.txt'
if not os.path.exists(filename):
os.system(r"touch {}".format(path))#调用系统命令行来创建文件