美文网首页
【linux】python 判断目录和文件是否存在,若不存在即创

【linux】python 判断目录和文件是否存在,若不存在即创

作者: 我的章鱼小丸子呢 | 来源:发表于2020-07-23 20:04 被阅读0次

判文件夹是否存在

import os
dirs = '/Users/joseph/work/python/'
if not os.path.exists(dirs):
    os.makedirs(dirs)

判断文件是否存在,不存在则会新建

import osfilename = '/Users/joseph/work/python/poem.txt'

if not os.path.exists(filename):
    os.system(r"touch {}".format(path))#调用系统命令行来创建文件

相关文章

网友评论

      本文标题:【linux】python 判断目录和文件是否存在,若不存在即创

      本文链接:https://www.haomeiwen.com/subject/nqfklktx.html