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

最新文章

  1. PHP 高级编程(5/5) - SPL 数组重载
  2. SqlServer-无限递归树状图结构设计和查询
  3. 国内可用maven repository 配置
  4. 结合stack数据结构,实现不同进制转换的算法
  5. Linux打包压缩.md
  6. HTML5+JS 《五子飞》游戏实现(二)路线分析和资源准备
  7. 杂-lowbit
  8. windows下安装redis以及简单的事例
  9. 贪心 POJ 1328 Radar Installation
  10. c#调用c++动态库的一些理解
  11. jq指定行切换
  12. StringBuilder是不是线程安全的?
  13. (转载)Java多线程入门理解
  14. Qemu,KVM,Virsh傻傻的分不清
  15. Tmux使用说明
  16. ORA-12520 TroubleShooting
  17. FPGA學習筆記(貳)--- 流水燈
  18. RTTI(运行时类型识别),typeid,dynamic_cast
  19. [Paper] Selection and replacement algorithm for memory performance improvement in Spark
  20. eclipse 运行简单JAVA程序事例

热门文章

  1. java基础:CompletionStage接口
  2. 08vue绑定用户页面
  3. leetcode_3FizzBuzz的一些思考
  4. Spring Boot 实现看门狗功能 (调用 Shell 脚本)
  5. Cys_Control(四) MTabControl
  6. Python中sorted(iterable, *, key=None, reverse=False)函数参数定义中的独立星号(*)的含义
  7. 第8.34节 《Python类中常用的特殊变量和方法》总结
  8. 第11.5节 Python正则表达式搜索任意字符匹配及元字符“.”(点)功能介绍
  9. CSS图标与文字对齐的两种方法
  10. LZZ磁力资源搜索4.2.2,整合多个站点,大部分资源都能搜到