Python创建系统目录的方法

yipeiwu_com6年前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程序设计有所帮助。

相关文章

Pyinstaller将py打包成exe的实例

Pyinstaller将py打包成exe的实例

背景:分享python编写的小脚本时,拷贝代码还缺各种环境,使用Pyinstaller将py可以打包成exe,直接运行即可 1、安装pyinstaller运行时所需要的windows拓展...

用python代码做configure文件

(在lua中通过loadfile, setfenv实现) python当然也可以: cat config.py bar = 10 foo=100 cat python_as_con...

Python向excel中写入数据的方法

Python向excel中写入数据的方法

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

Pyramid将models.py文件的内容分布到多个文件的方法

我们通过下面的文件结构,将models.py改成一个package. 复制代码 代码如下:myapp    __init__.py  &...

简单了解python 生成器 列表推导式 生成器表达式

生成器就是自己用python代码写的迭代器,生成器的本质就是迭代器。 通过以下两种方式构建一个生成器: 1、通过生成器函数 2、生成器表达式 生成器函数: 函数 def f...