Python 写了一个批量生成文件夹和批量重命名的工具

演示

功能

1. 可以读取excel内容,使用excel单元格内容进行新建文件夹,和文件夹重命名

2. 可以自定义重命名

3. 等

代码

import os
from pathlib import Path
import xlwings as xw tipStr = '输入工作路径'
tipStr1 = '输入excel名称' # 主界面
def mainWindow():
os.system('cls')
print('1. 新建文件夹')
print('2. 文件夹重命名')
print('0. 退出')
print('输入命令号')
num = input()
if('1' == num or '2' == num):
return int(num)
elif '0' == num:
return 0
else:
return -1 # 最大最小值数字 def getMinMaxNum():
print('输入最小值')
min = 0
while True:
min = input()
# 判断是否是写数字
if min.isdigit():
min = int(min)
if -1 < min and 999 >= min:
break
else:
print('请输入0-999的数字')
continue
else:
print('请输入数字')
continue print('输入最大值')
max = 0
while True:
max = input()
if max.isdigit():
max = int(max)
if -1 < max and 999 >= max:
break
else:
print('请输入0-999的数字')
continue
else:
print('请输入数字')
continue
return min, max # 获取excel内容 def getExcelContent(column):
path = os.getcwd() print(tipStr1)
excelPath = ''
flag = 0
while True:
# 拼接excel路径
excelPath = path + '\\' + input()
if not excelPath.endswith('.xlsx'):
excelPath = excelPath + ".xlsx"
# 判断文件是否存在
if Path(excelPath).exists():
break
else:
if 3 <= flag:
input('错误次数超过3次,重新开始,回车继续')
return False, []
print('文件不存在,重新输入')
flag = flag + 1
# 指定不显示地打开Excel,读取Excel文件
app = xw.App(visible=False, add_book=False)
wb = app.books.open(excelPath) # 打开Excel文件
sheet = wb.sheets[0] # 选择第0个表单
# 获取表行数
sheetInfo = sheet.used_range
maxRow = sheetInfo.last_cell.row
# print('行数:', maxRow)
nameList = []
# 遍历行,保存行数据
for row in range(1, maxRow + 1):
value = sheet.range(str(column)+str(row)).value
if isinstance(value, float):
nameList.append(str(int(value)))
else:
nameList.append(value) # 关闭当前工作簿
wb.close()
# 退出excel程序
app.quit() return True, nameList # 获取起始数字 def getStartNum():
# 获取起始数字
startNum = 0
while True:
print('输入起始数字')
startNum = input()
if not startNum.isdigit():
input("请输入数字,回车继续")
continue
break
# 转换为int类型
startNum = int(startNum)
return startNum # 新建文件夹
def newDir():
# 插入标签
os.system('cls')
print('1. excel\'A\'新建 (说明:将从指定的excel\'A\'列读取内容,读取的单元格内容做为文件夹名称)')
print('2. 0-999序号新建 (说明:将新建指定序列的文件夹)')
print('3. xxx0-999前缀加序号新建 (例:我很帅1,我很帅2...)')
print('4. 0-999xxx序号加后缀新建 (例:1我很帅,2我很帅...)')
print('输入命令号')
order = input()
if '1' == order:
os.system('cls')
path = os.getcwd() state, nameList = getExcelContent('A')
if not state:
return
for name in nameList:
dirName = path + '\\' + name
if Path(dirName).exists():
continue
os.mkdir(dirName)
print('创建目录【%s】成功' % dirName)
input("回车继续")
elif '2' == order:
os.system('cls')
min, max = getMinMaxNum()
print('输入最小值')
for index in range(min, max + 1):
path = os.getcwd()
if Path(path + '\\' + str(index)).exists():
continue
os.mkdir(path + '\\' + str(index))
elif '3' == order:
os.system('cls')
print('输入前缀')
prefix = input()
# 获取最大最小值
min, max = getMinMaxNum()
# 创建文件夹
for index in range(min, max+1):
path = os.getcwd()
dirName = path + '\\' + prefix + str(index)
if Path(dirName).exists():
continue
os.mkdir(dirName)
elif '4' == order:
os.system('cls')
min, max = getMinMaxNum()
print('输入后缀')
stufix = input()
for index in range(min, max):
path = os.getcwd()
dirName = path + '\\' + str(index) + stufix
if Path(dirName).exists():
continue
os.mkdir(dirName)
else:
input('无效的命令,回车继续') # 文件夹重命名
def reName():
os.system('cls')
print('1. 从excel\'A\'列重命名')
print('2. 数字顺序重命名')
print('3. 前缀加数字顺序重命名')
print('4. 数字加后缀顺序重命名')
print('输入命令号')
order = input() if '1' == order:
# 当前路径
currentPath = os.getcwd()
# 获取所有目录
dirList = []
for item in os.listdir(currentPath):
if os.path.isdir(currentPath + '\\' + item):
dirList.append(currentPath + '\\' + item) # 判断当前文件夹有没有文件夹
if 0 == len(dirList):
input('当前目录不存在文件夹,请先创建')
return
# 获取excel内容
state, nameList = getExcelContent('A')
if not state:
return # 遍历所有文件夹进行重命名
for index in range(0, len(nameList)):
if index > len(dirList) - 1:
input('所有文件夹重命名完毕,多余excel内容将不执行,回车继续')
break
oldDirName = dirList[index]
newDirName = currentPath + '\\' + nameList[index]
os.rename(oldDirName, newDirName) elif '2' == order:
# 当前路径
currentPath = os.getcwd()
# 获取所有目录
dirList = []
for item in os.listdir(currentPath):
if os.path.isdir(currentPath + '\\' + item):
dirList.append(currentPath + '\\' + item) # 判断当前文件夹有没有文件夹
if 0 == len(dirList):
input('当前目录不存在文件夹,请先创建')
return # 获取起始数字
startNum = getStartNum() # 重命名
for dirName in dirList:
numb = startNum
newDirName = currentPath + '\\' + str(numb)
if Path(newDirName).exists():
input('起始数字文件夹已存在,请尝试其它,回车继续')
break
os.rename(dirName, newDirName)
startNum = startNum + 1 elif '3' == order:
# 当前路径
currentPath = os.getcwd()
# 获取所有目录
dirList = []
for item in os.listdir(currentPath):
if os.path.isdir(currentPath + '\\' + item):
dirList.append(currentPath + '\\' + item) # 判断当前文件夹有没有文件夹
if 0 == len(dirList):
input('当前目录不存在文件夹,请先创建')
return # 获取前缀
print('输入前缀')
preFix = input() # 获取起始数字
startNum = getStartNum() for name in dirList:
num = startNum
newDirName = currentPath + '\\' + preFix + str(num)
os.rename(name, newDirName)
startNum = startNum + 1 elif '4' == order:
# 当前路径
currentPath = os.getcwd()
# 获取所有目录
dirList = []
for item in os.listdir(currentPath):
if os.path.isdir(currentPath + '\\' + item):
dirList.append(currentPath + '\\' + item) # 判断当前文件夹有没有文件夹
if 0 == len(dirList):
input('当前目录不存在文件夹,请先创建')
return # 获取起始数字
startNum = getStartNum() # 获取后缀
print('输入后缀')
stufix = input() for name in dirList:
num = startNum
newDirName = currentPath + '\\' + str(num) + stufix
os.rename(name, newDirName)
startNum = startNum + 1 else:
input('无效的命令,回车继续') if __name__ == "__main__":
while(True):
num = mainWindow()
if -1 == num:
continue
elif 0 == num:
print('byebye and see you lala')
input('任意键退出')
break
elif 1 == num:
newDir()
elif 2 == num:
reName()

下载

https://download.csdn.net/download/ShiShiSoLo/13767713

Python 写了一个批量生成文件夹和批量重命名的工具的更多相关文章

  1. R8—批量生成文件夹,批量读取文件夹名称+R文件管理系统操作函数

    一. 批量生成文件夹,批量读取文件夹名称 今日,工作中遇到这样一个问题:boss给我们提供了200多家公司的ID代码(如6007.7920等),需要根据这些ID号去搜索下载新闻,从而将下载到的新闻存到 ...

  2. windows下批量生成文件夹

    在windows环境下如果想要批量生成文件夹: 1.创建一个记事本文件 2.首行大写MD 3.后面加上你想创建的文件夹的名字,每个名字之间有空格 4.退出记事本并保存 5.将记事本文件后缀改为bat文 ...

  3. 批量生成文件夹内所有文件md5

    说明:md5批量生成批处理脚本,无需安装任何软件,直接调用系统文件进行生成,简单基于windows命令编写了一个批量生成md5值的脚本. 使用说明:新建文本文档,命名为get_md5.bat,直接将代 ...

  4. python将指定目录下的所有文件夹用随机数重命名

    我的目的在于打乱数据顺序,便于GAN训练: import random import os path = 'hunhe_7' #目标文件夹 listname = os.listdir(path) #遍 ...

  5. [Batch 脚本] 批量生成文件夹

    @echo off echo start set time=30000 echo %time% for /l %%i in (1,1, %time%) do ( echo %%i% md " ...

  6. 「懒惰的美德」我用 python 写了个自动生成给文档生成索引的脚本

    我用 python 写了一个自动生成索引的脚本 简介:为了刷算法题,建了一个 GitHub仓库:PiperLiu / ACMOI_Journey,记录自己的刷题轨迹,并总结一下方法.心得.想到一个需求 ...

  7. 用python脚本通过excel生成文件夹树结构

    大概这样写标题是对的吧... 目标: 通过excel目录结构文档生成文件夹树结构. 也就是: 通过下面的excel

  8. python之对指定目录文件夹的批量重命名

    python之对指定目录文件夹的批量重命名 import os,shutil,string dir = "/Users/lee0oo0/Documents/python/test" ...

  9. 使用JFileChooser实现在指定文件夹下批量添加根据“数字型样式”或“非数字型样式”命令的文件夹

    2018-11-05 20:57:00开始写 Folder.java类 import javax.swing.JFrame; import javax.swing.JPanel; import jav ...

随机推荐

  1. 使用logisim搭建单周期CPU与添加指令

    使用logisim搭建单周期CPU与添加指令 搭建 总设计 借用高老板的图,我们只需要分别做出PC.NPC.IM.RF.EXT.ALU.DM.Controller模块即可,再按图连线,最后进行控制信号 ...

  2. rest-framework 解析器

    一 解析器的作用: 根据请求头 content-type 选择对应的解析器对请求体内容进行处理. 有application/json,x-www-form-urlencoded,form-data等格 ...

  3. Django----Serializer序列化

    serializer的两大特征 1.校检数据 2.序列化 首先创建apps/Serializer.py 在序列化里面导包 from rest_framework import serializers ...

  4. idea使用帮助

    IDEA激活码形式,扫码二维码回复 激活码 自提,秒激活,持续更新.回复的是> 激活码 2020.2以上版本的 IDEA 请跳转至该链接:https://t.1yb.co/3ntg 2018.3 ...

  5. 使用Docker快速部署各类服务

    使用Docker快速部署各类服务 一键安装Docker #Centos环境 wget -O- https://gitee.com/iubest/dinstall/raw/master/install. ...

  6. charles 常用功能(七)简易接口压力测试(repeat advance 功能)

    接口请求次数.并发量.请求延迟时间均可配置 1.选中需要进行测试的接口,鼠标右键 选中[repeat advance] 设置迭代数量

  7. 九. Vuex详解

    1. 理解Vuex 1.1 Vuex功能 官方解释 Vuex 是一个专为 Vue.js 应用程序开发的状态管理模式.它采用 集中式存储 管理应用的所有组件的状态,并以相应的规则保证状态以一种可预测的方 ...

  8. 第8.9节 Python类中内置的查看直接父类的__bases__属性

    终于介绍完了__init__方法和__new__方法,接下来轻松一下,本节介绍类中内置的__bases__属性. 一. 语法释义 Python 为所有类都提供了一个 bases 属性,通过该属性可以查 ...

  9. Python正则表达式\W+和\W*匹配过程的深入分析

    在学习re.split函数的处理过程中,发现执行如下语句及返回与老猿预想的不一致: >>> re.split('\W*','Hello,world') ['', 'H', 'e', ...

  10. 第14.9节 Python中使用urllib.request+BeautifulSoup获取url访问的基本信息

    利用urllib.request读取url文档的内容并使用BeautifulSoup解析后,可以通过一些基本的BeautifulSoup对象输出html文档的基本信息.以博文<第14.6节 使用 ...