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利用sqlacodegen自动生成ORM实体类示例

本文实例讲述了Python利用sqlacodegen自动生成ORM实体类。分享给大家供大家参考,具体如下: 在前面一篇《Python流行ORM框架sqlalchemy安装与使用》我们是手...

pytorch AvgPool2d函数使用详解

我就废话不多说了,直接上代码吧! import torch import torch.nn as nn import torch.nn.functional as F from to...

Python设计模式编程中解释器模式的简单程序示例分享

Python设计模式编程中解释器模式的简单程序示例分享

模式特点:给定一个语言,定义它的文法的一种表示,并定义一个解释器,这个解释器使用该表示来解释语言中的句子。 我们来看一下下面这样的程序结构: class Context: de...

基于Python实现用户管理系统

基于Python的用户管理小系统,包含文件读写操作,实现了用户信息注册和登录的简单功能。 class userLogReg: """ Created on 2018.11...

如何用Python做一个微信机器人自动拉群

如何用Python做一个微信机器人自动拉群

引言 微信群的用户添加逻辑是 —— 当群人数达到100人后,用户无法再通过扫描群二维码加入,只能让用户先添加群内联系人微信,再由联系人把用户拉进来。这样,联系人员的私人微信会添加大量陌...