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中global语句下全局变量的值的修改

Python的全局变量:int string, list, dic(map) 如果存在global就能够修改它的值。而不管这个global是否是存在于if中,也不管这个if是否能够执行到...

Python向excel中写入数据的方法

Python向excel中写入数据的方法

最近做了一项工作需要把处理的数据写入到Excel表格中进行保存,所以在此就简单介绍使用Python如何把数据保存到excel表格中。 数据导入之前需要安装 xlwt依赖包,安装的方法就很...

深入解析Python中函数的参数与作用域

传递参数 函数传递参数时的一些简要的关键点: 参数的传递是通过自动将对象赋值给本地变量名来实现的。所有的参数实际上都是通过指针进行传递的,作为参数被传递的对象从来不自动拷贝。...

ipython和python区别详解

ipython和python区别详解

ipython介绍 IPython 是一个 python 的交互式 shell,比默认的python shell 好用得多,支持变量自动补全,自动缩进,支持 bash shell命令,内...

用python解压分析jar包实例

写这个玩意的背景:在u8多渠道打包里,需要分析jar包,并把里面的文件按目录和类型分别放在root和assets文件夹里,之前师兄都是手动解压,一个一个文件夹找文件,效率比较低,刚好最近...