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

演示

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

功能

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

12-24 10:10