[os/Python]基于os的大批量文件遍历重命名系统V1

——【目录】—— 


一、import os库

二、自定义遍历文件函数

三、输入文件夹路径

四、输出文件数量 

五、特殊化命名

六、数字化命名

七、完成提示


一、import os库 

import os

 二、自定义遍历文件函数

# 遍历文件函数
def getFileList(dir, Filelist, ext=None):
    """
    获取文件夹及其子文件夹中文件列表
    输入 dir:文件夹根目录
    输入 ext: 扩展名
    返回: 文件路径列表
    """
    newDir = dir
    if os.path.isfile(dir):
        if ext is None:
            Filelist.append(dir)
        else:
            if ext in dir[-3:]:
                Filelist.append(dir)

    elif os.path.isdir(dir):
        for s in os.listdir(dir):
            newDir = os.path.join(dir, s)
            getFileList(newDir, Filelist, ext)

    return Filelist

 三、输入文件夹路径

path_image = 'D:\pythonProject4_camera\\beforeGroundTruthBoxImage'

 四、输出文件数量 

imgfolder = path_image  # 搜索路径,会自动搜索子文件夹
imglist = getFileList(imgfolder, [])  # 取出文件路径,为字符串类型数组
print("\n检测到" + imgfolder + "下有" + str(len(imglist)) + '个文件')  # 输出文件数量

 五、特殊化命名

imgfolder = path_image  # 搜索路径,会自动搜索子文件夹
imglist = getFileList(imgfolder, [])  # 取出文件路径,为字符串类型数组
print("\n检测到" + imgfolder + "下有" + str(len(imglist)) + '个文件')  # 输出文件数量
img_num = 0
for imgpath in imglist:  # 防止重命名时与其他文件原名重复,先把所有文件命名为绝对不会重复的名字
    img_num += 1
    loc_dot = 0
    for i in range(len(imgpath), 1, -1):
        if (imgpath[i - 1]) == ".":
            loc_dot = i
            break
    os.rename(src=imgpath, dst=path_image + "\\" + "TMP_WARNING" + str(img_num) + imgpath[i - 1:])

 六、数字化命名

imgfolder = path_image  # 搜索路径,会自动搜索子文件夹
imglist = getFileList(imgfolder, [])  # 取出文件路径,为字符串类型数组
img_num = 0
for imgpath in imglist:  # 遍历按数字索引重命名
    img_num += 1
    loc_dot = 0
    for i in range(len(imgpath), 1, -1):
        if (imgpath[i - 1]) == ".":
            loc_dot = i
            break
    os.rename(src=imgpath, dst=path_image + "\\" + str(img_num) + imgpath[i - 1:])

 七、完成提示

print("\n重命名已完成,请自行检查文件夹内文件名")

源码提供:

import os


# 遍历文件函数
def getFileList(dir, Filelist, ext=None):
    """
    获取文件夹及其子文件夹中文件列表
    输入 dir:文件夹根目录
    输入 ext: 扩展名
    返回: 文件路径列表
    """
    newDir = dir
    if os.path.isfile(dir):
        if ext is None:
            Filelist.append(dir)
        else:
            if ext in dir[-3:]:
                Filelist.append(dir)

    elif os.path.isdir(dir):
        for s in os.listdir(dir):
            newDir = os.path.join(dir, s)
            getFileList(newDir, Filelist, ext)

    return Filelist


path_image = 'D:\pythonProject4_camera\\beforeGroundTruthBoxImage'

imgfolder = path_image  # 搜索路径,会自动搜索子文件夹
imglist = getFileList(imgfolder, [])  # 取出文件路径,为字符串类型数组
print("\n检测到" + imgfolder + "下有" + str(len(imglist)) + '个文件')  # 输出文件数量
img_num = 0
for imgpath in imglist:  # 防止重命名时与其他文件原名重复,先把所有文件命名为绝对不会重复的名字
    img_num += 1
    loc_dot = 0
    for i in range(len(imgpath), 1, -1):
        if (imgpath[i - 1]) == ".":
            loc_dot = i
            break
    os.rename(src=imgpath, dst=path_image + "\\" + "TMP_WARNING" + str(img_num) + imgpath[i - 1:])

imgfolder = path_image  # 搜索路径,会自动搜索子文件夹
imglist = getFileList(imgfolder, [])  # 取出文件路径,为字符串类型数组
img_num = 0
for imgpath in imglist:  # 遍历按数字索引重命名
    img_num += 1
    loc_dot = 0
    for i in range(len(imgpath), 1, -1):
        if (imgpath[i - 1]) == ".":
            loc_dot = i
            break
    os.rename(src=imgpath, dst=path_image + "\\" + str(img_num) + imgpath[i - 1:])

print("\n重命名已完成,请自行检查文件夹内文件名")

(~▽~~)(~~▽~)(~▽~~)(~~▽~)(~▽~~)(~~▽~)(~▽~~)(~~▽~)(~▽~~)(~~▽~)(~▽~~)(~~▽~)(~▽~~)(~~▽~)(~▽~~)(~~▽~)(~▽~~)(~~▽~)(~▽~~)(~~▽~)(~▽~~)(~~▽~)(~▽~~)(~~▽~)(~▽~~)(~~▽~)(~▽~~)(~~▽~)(~▽~~)(~~▽~)(~▽~~)(~~▽~)(~▽~~)(~~▽~)(~▽~~)(~~▽~)(~▽~~)(~~▽~)(~▽~~)(~~▽~)(~▽~~)(~~▽~)(~▽~~)(~~▽~)(~▽~~)(~~▽~)(~▽~~)(~~▽~)(~▽~~)(~~▽~)(~▽~~)(~~▽~)(~▽~~)(~~▽~)(~▽~~)(~~▽~)(~▽~~)(~~▽~)(~▽~~)(~~▽~)(~▽~~)(~~▽~)(~▽~~)(~~▽~)(~▽~~)(~~▽~)(~▽~~)(~~▽~)(~▽~~)(~~▽~)(~▽~~)(~~▽~)(~▽~~)(~~▽~)(~▽~~)(~~▽~)(~▽~~)(~~▽~)(~▽~~)(~~▽~)(~▽~~)(~~▽~)(~▽~~)(~~▽~)(~▽~~)(~~▽~)(~▽~~)(~~▽~)(~▽~~)(~~▽~)(~▽~~)(~~▽~)(~▽~~)(~~▽~)(~▽~~)(~~▽~)(~▽~~)(~~▽~)(~▽~~)(~~▽~)(~▽~~)(~~▽~)(~▽~~)(~~▽~)(~▽~~)(~~▽~)(~▽~~)(~~▽~)(~▽~~)(~~▽~)(~▽~~)(~~▽~)(~▽~~)(~~▽~)(~▽~~)(~~▽~)(~▽~~)(~~▽~)(~▽~~)(~~▽~)(~▽~~)(~~▽~)(~▽~~)(~~▽~)(~▽~~)(~~▽~)(~▽~~)(~~▽~)(~▽~~)(~~▽~)(~▽~~)(~~▽~)(~▽~~)(~~▽~)(~▽~~)(~~▽~)(~▽~~)(~~▽~)(~▽~~)(~~▽~)(~▽~~)(~~▽~)(~▽~~)(~~▽~)(~▽~~)(~~▽~)(~▽~~)(~~▽~)(~▽~~)(~~▽~)(~▽~~)(~~▽~)(~▽~~)(~~▽~)(~▽~~)(~~▽~)(~▽~~)(~~▽~)(~▽~~)(~~▽~)(~▽~~)(~~▽~)(~▽~~)(~~▽~)(~▽~~)(~~▽~)(~▽~~)(~~▽~)(~▽~~)(~~▽~)(~▽~~)(~~▽~)(~▽~~)(~~▽~)(~▽~~)(~~▽~)(~▽~~)(~~▽~)(~▽~~)(~~▽~)(~▽~~)(~~▽~)(~▽~~)(~~▽~)(~▽~~)(~~▽~)(~▽~~)(~~▽~)(~▽~~)(~~▽~)(~▽~~)(~~▽~) 

你可能感兴趣的:(Python,python)