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

相关文章

pycharm 解除默认unittest模式的方法

pycharm 解除默认unittest模式的方法

pycharm关闭unittest模式方法 网上看到了很多方法,尝试之后偶然奏效,该方法确定可以关闭单元测试: 点击如图所示的工具图标,Tools-python integrated...

python实现机器学习之多元线性回归

python实现机器学习之多元线性回归

总体思路与一元线性回归思想一样,现在将数据以矩阵形式进行运算,更加方便。 一元线性回归实现代码 下面是多元线性回归用Python实现的代码: import numpy as np...

pandas 层次化索引的实现方法

层次化索引是pandas的一项重要功能,它使你能在一个轴上拥有多个(两个以上)索引级别。 创建一个Series,并用一个由列表或数组组成的列表作为索引。 data=Series(np...

python 字典 按key值大小 倒序取值的实例

如下所示: viedoUrl_dict = {1:'hello',2:'python',3:'nihao'} bit_list = sorted(viedoUrl_dict.keys...

python 时间戳与格式化时间的转化实现代码

python 里面与时间有关的模块主要是 time 和 datetime 如果想获取系统当前时间戳:time.time() ,是一个float型的数据 获取系统当前的时间信息 : tim...