基于Python实现批量创建目录

by:授客QQ1033553122

测试环境:

Python版本:Python 2.7

 

代码实践

#!/usr/bin/env python

# -*- coding:utf-8 -*-

__author__ = 'shouke'

import os

class PublicTools:

def __init__(self):

pass

# 批量创建目录

def mkdirs_once_many(self, path):

path = os.path.normpath(path)  # 去掉路径最右侧的 \\ 、/

path = path.replace('\\', '/') # 将所有的\\转为/,避免出现转义字符串

head, tail = os.path.split(path)

new_dir_path = ''  # 反转后的目录路径

root = ''  #根目录

if not os.path.isdir(path) and os.path.isfile(path):  # 如果path指向的是文件,则继续分解文件所在目录

head, tail = os.path.split(head)

if tail == '':

return

while tail:

new_dir_path = new_dir_path + tail + '/'

head, tail = os.path.split(head)

root = head

else:

new_dir_path = root + new_dir_path

# print(new_dir_path)

# 批量创建目录

new_dir_path = os.path.normpath(new_dir_path)

head, tail = os.path.split(new_dir_path)

temp = ''

while tail:

temp = temp + '/' + tail

dir_path = root + temp

if not os.path.isdir(dir_path):

os.mkdir(dir_path)

head, tail = os.path.split(head)

# if __name__ == '__main__':

#     # file_name = 'D:\\tset\\tkise\\FAQ.txt'

#     # mkdirs_once_many(file_name)

#     # file_name = 'D:\\tset\\tkise\\'

#     # mkdirs_once_many(file_name)

#     # file_name = 'd:\\'

#     # mkdirs_once_many(file_name)

#     file_name = r'D:\tset\tkise\FAQ.txt'

#     mkdirs_once_many(file_name)

#     #file_name = '/tset/tkise/' # Linux下应该可以/未验证

#     #mkdirs_once_many(file_name)

Python 基于Python实现批量创建目录的更多相关文章

  1. Python 基于Python实现的ssh兼sftp客户端(上)

    基于Python实现的ssh兼sftp客户端   by:授客 QQ:1033553122 实现功能 实现ssh客户端兼ftp客户端:实现远程连接,执行linux命令,上传下载文件 测试环境 Win7 ...

  2. Python基于Python实现批量上传文件或目录到不同的Linux服务器

    基于Python实现批量上传文件或目录到不同的Linux服务器   by:授客 QQ:1033553122 实现功能 1 测试环境 1 使用方法 1 1. 编辑配置文件conf/rootpath_fo ...

  3. Python 基于Python实现的ssh兼sftp客户端(下)

    基于Python实现的ssh兼sftp客户端   by:授客 QQ:1033553122 otherTools.py #!/usr/bin/env/ python # -*- coding:utf-8 ...

  4. Python 基于Python及zookeeper实现简单分布式任务调度系统设计思路及核心代码实现

    基于Python及zookeeper实现简单分布式任务调度系统设计思路及核心代码实现   by:授客 QQ:1033553122 测试环境 功能需求 实现思路 代码实践(关键技术点实现) 代码模块组织 ...

  5. Python 基于python操纵zookeeper介绍

    基于python操纵zookeeper介绍 by:授客  QQ:1033553122 测试环境 Win7 64位 Python 3.3.4 kazoo-2.6.1-py2.py3-none-any.w ...

  6. Python 基于Python结合pykafka实现kafka生产及消费速率&主题分区偏移实时监控

    基于Python结合pykafka实现kafka生产及消费速率&主题分区偏移实时监控   By: 授客 QQ:1033553122   1.测试环境 python 3.4 zookeeper- ...

  7. Python 基于Python从mysql表读取千万数据实践

    基于Python 从mysql表读取千万数据实践   by:授客 QQ:1033553122 场景:   有以下两个表,两者都有一个表字段,名为waybill_no,我们需要从tl_waybill_b ...

  8. Python 基于python编写一些算法程序等

    基于python编写一些算法程序等 by:授客 QQ:1033553122 QQ群:7156436 没特意去研究,只是这对群友在QQ群里(7156436)提出的一些小程序实现.编程题,算法.问题等,本 ...

  9. Python 基于python+mysql浅谈redis缓存设计与数据库关联数据处理

    基于python+mysql浅谈redis缓存设计与数据库关联数据处理 by:授客  QQ:1033553122 测试环境 redis-3.0.7 CentOS 6.5-x86_64 python 3 ...

随机推荐

  1. Java学习笔记31(集合框架五:set接口、哈希表的介绍)

    set接口的特点: 1.不包含重复元素 2.set集合没有索引,只能用迭代器或增强for循环遍历 3.set的底层是map集合 方法和Collection的方法基本一样 set接口的实现类HashSe ...

  2. Shell-6--预定义变量

  3. 纯css实现不同方向的三角形

    .triangle { position: relative; &:after { position: absolute; top: 50%; transform: translate(0,- ...

  4. Python——通过用户cookies访问微博首页

    通过用户cookies访问微博首页 1.登录微博 self.driver.delete_all_cookies() # 删除cookies self.driver.get(self.url) time ...

  5. win10 store 无法连接网络(原创)

    当你试过所有的解决攻略 都无效时,那么使用这个教程 关闭以下的蓝色框里的

  6. C# 窗体间传值

    Form1: 父窗体, Form2: 子窗体. 1.父窗体接收子窗体的返回值: public partial class Form1: Form { private void btnOpen_Clic ...

  7. Spring Session - 使用Redis存储HttpSession例子

    目的 使用Redis存储管理HttpSession: 添加pom.xml 该工程基于Spring Boot,同时我们将使用Spring IO Platform来维护依赖版本号: 引入的依赖有sprin ...

  8. LeetCode刷题指南(字符串)

    作者:CYC2018 文章链接:https://github.com/CyC2018/CS-Notes/blob/master/docs/notes/Leetcode+%E9%A2%98%E8%A7% ...

  9. TCP/IP 笔记 - 用户数据报协议和IP分片

    关于本章中的IP分片部分,参考第五章IP分片头部知识点.需要注意的是,TCP有超时重传,UDP的超时重传则依赖上层应用程序实现. 用户数据报协议(UDP) UDP是一个简单的面向无连接.不可靠的数据报 ...

  10. rabbitMQ和对应的erlang版本匹配

    来源自https://www.rabbitmq.com/which-erlang.html erlang安装包下载地址 https://packages.erlang-solutions.com/er ...