Python 写了一个批量生成文件夹和批量重命名的工具
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 写了一个批量生成文件夹和批量重命名的工具的更多相关文章
- R8—批量生成文件夹,批量读取文件夹名称+R文件管理系统操作函数
一. 批量生成文件夹,批量读取文件夹名称 今日,工作中遇到这样一个问题:boss给我们提供了200多家公司的ID代码(如6007.7920等),需要根据这些ID号去搜索下载新闻,从而将下载到的新闻存到 ...
- windows下批量生成文件夹
在windows环境下如果想要批量生成文件夹: 1.创建一个记事本文件 2.首行大写MD 3.后面加上你想创建的文件夹的名字,每个名字之间有空格 4.退出记事本并保存 5.将记事本文件后缀改为bat文 ...
- 批量生成文件夹内所有文件md5
说明:md5批量生成批处理脚本,无需安装任何软件,直接调用系统文件进行生成,简单基于windows命令编写了一个批量生成md5值的脚本. 使用说明:新建文本文档,命名为get_md5.bat,直接将代 ...
- python将指定目录下的所有文件夹用随机数重命名
我的目的在于打乱数据顺序,便于GAN训练: import random import os path = 'hunhe_7' #目标文件夹 listname = os.listdir(path) #遍 ...
- [Batch 脚本] 批量生成文件夹
@echo off echo start set time=30000 echo %time% for /l %%i in (1,1, %time%) do ( echo %%i% md " ...
- 「懒惰的美德」我用 python 写了个自动生成给文档生成索引的脚本
我用 python 写了一个自动生成索引的脚本 简介:为了刷算法题,建了一个 GitHub仓库:PiperLiu / ACMOI_Journey,记录自己的刷题轨迹,并总结一下方法.心得.想到一个需求 ...
- 用python脚本通过excel生成文件夹树结构
大概这样写标题是对的吧... 目标: 通过excel目录结构文档生成文件夹树结构. 也就是: 通过下面的excel
- python之对指定目录文件夹的批量重命名
python之对指定目录文件夹的批量重命名 import os,shutil,string dir = "/Users/lee0oo0/Documents/python/test" ...
- 使用JFileChooser实现在指定文件夹下批量添加根据“数字型样式”或“非数字型样式”命令的文件夹
2018-11-05 20:57:00开始写 Folder.java类 import javax.swing.JFrame; import javax.swing.JPanel; import jav ...
随机推荐
- UIWebView各种加载网页的方式
UIWebView加载网页的方法 最近在使用UIWebView的时候遇到各种不同形式加载网页的方式,总结起来共有三种方式,分别为:使用URL加载,使用HTML源码加载,使用HTML文件加载,各种方法的 ...
- iOS问题:pch not found
问题描述: clang: error: no such file or directory: '/Users/apple/Desktop/迅点App_Mark/FaceHelp/FaceHelp-Pr ...
- yii2.0上传图片
model: 1 use Yii; 2 public $imageUpload; 3 public function rules() 4 { 5 return [ 6 [['imageUpload'] ...
- Linux没有netstat和ifconfig命令问题
安装了精简版命令行centos7系统,运行netsta查看端口和ifconfig查看IP命令都提示找不到命令. 解决方法: yum search ifconfig 通过yum search 这个命令我 ...
- Pytest自动化测试 - 必知必会的一些插件
Pytest拥有丰富的插件架构,超过800个以上的外部插件和活跃的社区,在PyPI项目中以" pytest- *"为标识. 本篇将列举github标星超过两百的一些插件进行实战演示 ...
- 解决linux挖矿病毒(kdevtmpfsi,sysupdate, networkservice)
突然发现公司测试服务器CPU过高,是这两个sysupdate, networkservice进程,很明显是被挖矿了,记录下来以供参考. 病毒会把一些文件给加i锁或a锁,导致无法修改数据,所以某些操作需 ...
- ios真机使用fixed定位页面滚动时fixed定位的元素也会跟着滚动
到了ios真机APP中,页面向下滚动,fixed的元素也跟着滚,虽然最后它还是到了它该在的地方,但是它跟着滚动也很影响页面的流畅性和交互性好伐.
- python核心高级学习总结4-------python实现进程通信
Queue的使用 Queue在数据结构中也接触过,在操作系统里面叫消息队列. 使用示例 # coding=utf-8 from multiprocessing import Queue q = Que ...
- 第10.7节 Python包和子包的定义步骤
一. 包的定义步骤 按照包名创建或使用一个已有目录,目录名就是包名,必须注意包的目录必须位于Python加载模块的搜索路径中(具体请参考<第10.1节 Python的模块及模块导入>关于模 ...
- JAVA课堂作业(2019.10.14)
一. (1)代码 package class20191014; import java.util.Scanner; public class ClassHomework { public static ...