• 首页

  • 官方

  • 主题

  • 关注

  • 联系

python批量更改文件名并移动到新的文件夹

python批量更改文件名并移动到新的文件夹

前言:
1. 这里的代码以批量命名如:
路径"E:\下载\1\xxxx.mp4"、"E:\下载\2\xxxx.mp4"......
为"E:\下载\1\1.mp4"、"E:\下载\2\2.mp4"
并移动到路径"E:\download"的格式为例
2. 得学会举一反三
# 批量修改文件名,并移动到新的文件夹
import os
import re
import shutil

file = os.listdir(r"E:\下载")  # 待修改文件夹
for i in range(len(file)):
    path = "E:\下载\\592237440\\" + str(i + 1)
    fileList = os.listdir(path)  # 待修改文件目录
    currentList = os.getcwd()  # 获得当前工作目录
    os.chdir(path)  # 切换当前工作目录为待修改文件夹
    matching = ".+\.mp4"  # 匹配文件名正则表达式
    for fileName in fileList:
        modification = re.findall(matching, fileName)  # 进行匹配
        if modification:
            print(modification[0])
            os.rename(modification[0], (str(i + 1) + '.mp4'))  # 文件重新命名
            currentPath = path+'\\'+str(i + 1) + '.mp4'
            destinationPath = "E:\\download"
            shutil.move(currentPath, destinationPath)  # 移动文件到目标路径


posted @ 2021-12-28 13:40  戈小戈  阅读(310)  评论(0编辑  收藏  举报