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. C语言讲义——快速排序

    快速排序是C.R.A.Hoare于1962年提出的一种划分交换排序 它采用了一种分治的策略,通常称其为分治法(Divide-and-ConquerMethod) 基本思想: 1.先从数列中取出一个数作 ...

  2. zk特性

    看了又忘系列: 1.zk会将全量的数据存储在内存中,以此来实现提高服务器吞吐,减少延迟的目的. 2.集群中每台机器都会在内存中维护当前的服务器状态,并且每台机器之间都相互保持着通信.只要集群中存在超过 ...

  3. dubbo协议之编码请求对象体

    上节我们看了如何编码请求头,这节一起看下过程中,对请求对象的编码,涉及对接口,方法,方法参数类型,方法参数进行编码,DubboCodec中重写了这个方法: request.getData向下转型成Rp ...

  4. react高阶组件的一些运用

    今天学习了react高阶组件,刚接触react学习起来还是比较困难,和大家分享一下今天学习的知识吧,另外缺少的地方欢迎补充哈哈 高阶组件(Higher Order Components,简称:HOC) ...

  5. Jmeter测试Websocket接口

    前言 websocket是什么? WebSocket 协议在2008年诞生,2011年成为国际标准.所有浏览器都已经支持了. 它的最大特点就是,服务器可以主动向客户端推送信息,客户端也可以主动向服务器 ...

  6. 使用 Jasypt 加密 Spring Boot 配置文件

    一.添加依赖包 <dependency> <groupId>com.github.ulisesbocchio</groupId> <artifactId> ...

  7. 20200116_centos7.2 下 mysql_5.7修改root密码

    1. 需改my.cnf文件 [root@rakinda-iot-platform ~]# vim /etc/my.cnf 2. 新增一行, 登录时跳过密码, 保存后退出, 重启mysql system ...

  8. 从七牛云迁移图片到github

    迁移理由 问题是网站的大部分图床都是用的七牛云,官网有改动,所以原测试域名都失效,所以决定进行迁移,将七牛云中的图片迁移到github仓库中. 迁移步骤 Step1:从废弃测试域名空间至可用测试域名空 ...

  9. 第4.2节 神秘而强大的Python生成器精讲

    一. 生成器(generator)概念 生成器是一个特殊的迭代器,它保存的是算法,每次调用next()或send()就计算出下一个元素的值,直到计算出最后一个元素,没有更多的元素时,抛出StopIte ...

  10. Python中str类型的字符串写入二进制文件时报TypeError错的处理方式

    在用二进制模式打开文件情况下,写入一个str对象时报错:TypeError: a bytes-like object is required, not 'str' 出现该问题是因为Python严格区分 ...