gpt4 book ai didi

Python创建系统目录的方法

转载 作者:qq735679552 更新时间:2022-09-29 22:32:09 26 4
gpt4 key购买 nike

CFSDN坚持开源创造价值,我们致力于搭建一个资源共享平台,让每一个IT人在这里找到属于你的精彩世界.

这篇CFSDN的博客文章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创建系统目录的方法的内容请搜索CFSDN的文章或继续浏览相关文章,希望大家以后支持我的博客! 。

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com