Python创建系统目录的方法

yipeiwu_com5年前Python基础

本文实例讲述了Python创建系统目录的方法。分享给大家供大家参考。具体如下:

Python2 mkdir在没有上级目录时创建会失败.该方法可以创建多级目录。
/temp/gapgers/upload/images/1.png
如过temp文件夹不存在,会创建空的文件夹/temp/gapgers/upload/images/以及空文件1.png。
该方法只做抛砖引玉,大神勿喷

复制代码 代码如下:
import os
    def mkfilePower(path):
      '''create dirs if the path contain a file create a empty file
      if the dir's file is exist return False else return True
      ex:path = r'c:/temp/gapgers/upload/images/1.png'
      nomatter there have dir temp or not,we will create it and create a empty file 1.png
      '''
      paths = path.split('/')
      temppath = ''
      for index,_spilt in enumerate(paths):
          if index == 0:
              temppath = _spilt
              continue
          temppath = temppath + '/' + _spilt
          if os.path.isdir(temppath):
              pass
          elif index == len(paths)-1:
              if os.path.isfile(temppath):
                  return False
              fl = open(temppath,'w')
              fl.close()
          else:
              os.mkdir(temppath)
      return True

希望本文所述对大家的Python程序设计有所帮助。

相关文章

详解python中的文件与目录操作

详解python中的文件与目录操作 一 获得当前路径 1、代码1 >>>import os >>>print('Current directo...

跟老齐学Python之Python文档

跟老齐学Python之Python文档

文档很重要。独孤九剑的剑诀、易筋经的心法、写着辟邪剑谱的袈裟,这些都是文档。连那些大牛人都要这些文档,更何况我们呢?所以,文档是很重要的。 文档,说白了就是用word(这个最多了)等(注...

python 控制语句

1比如python提倡简单实用的思想,它就没有switch语句,如果要实现switch语句的效果 的话可以通过2个方法来写把 (1)通过if elif 语句来实现 if 条件: … el...

Python matplotlib 画图窗口显示到gui或者控制台的实例

我们再用Jupyter-notebook,ipython-console,qtconsole的时候,有的时候画图希望不弹出窗口,直接画在console里,又得时候有希望弹出窗口,因为co...

实例介绍Python中整型

Python中有以下几个基本的数据类型: 整数 int 字符串 str 浮点数 float 集合 set 列表 list 元组 tuple 字典 dict...