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自动连接ssh的方法

本文实例讲述了Python自动连接ssh的方法。分享给大家供大家参考。具体实现方法如下: #!/usr/bin/python #-*- coding:utf-8 -*- import...

python cv2读取rtsp实时码流按时生成连续视频文件方式

python cv2读取rtsp实时码流按时生成连续视频文件方式

我就废话不多说了,直接上代码吧! # coding: utf-8 import datetime import cv2 import os ip = '192.168.3.160...

Pytorch 的损失函数Loss function使用详解

Pytorch 的损失函数Loss function使用详解

1.损失函数 损失函数,又叫目标函数,是编译一个神经网络模型必须的两个要素之一。另一个必不可少的要素是优化器。 损失函数是指用于计算标签值和预测值之间差异的函数,在机器学习过程中,有多种...

深入解析python中的实例方法、类方法和静态方法

深入解析python中的实例方法、类方法和静态方法

1、实例方法/对象方法 实例方法或者叫对象方法,指的是我们在类中定义的普通方法。 只有实例化对象之后才可以使用的方法,该方法的第一个形参接收的一定是对象本身 2、静态方法 (1).格式...

python使用socket 先读取长度,在读取报文内容示例

本文实例讲述了python使用socket 先读取长度,在读取报文内容。分享给大家供大家参考,具体如下: tlpmts1:~/sbin # cat test9105.py # -*-...