美文网首页
python检查目录是否存在,如果不存在则创建一个

python检查目录是否存在,如果不存在则创建一个

作者: 洛丽塔的云裳 | 来源:发表于2019-10-15 20:32 被阅读0次
方法1:这个很简单其实就是考察os.path.exists()的使用
# -*- coding: utf-8 -*-

import os
import sys


def dir_test(testdirs):
    """ Tests to see if the directory testdir exists, if not it will create the directory for you. """
    if not os.path.exists(testdirs):
        os.makedirs(testdirs)


if __name__ == '__main__':
    testdirs = '/home/work/test/world'
    dir_test(testdirs)
test.png

基本与github给出的一致

https://github.com/geekcomputers/Python/blob/master/dir_test.py

相关文章

网友评论

      本文标题:python检查目录是否存在,如果不存在则创建一个

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