python3批量为文件重命名

简介

为什么使用python批量重命名而没有使用shell,python对文字的处理更灵活,对于文件名中的空格不会出现报错。

代码:

#!/usr/bin/env python
# -*- coding:utf-8 -*-
"""
@author:Aiker Zhao
@file:rename_all.py
@time:下午10:29
"""

import os

path = "M:\\bd1\\都挺好\\"
new_path = "M:\\bd1\\doutinghao\\"
for file in os.listdir(path):
    # print(file)
    if os.path.isfile(os.path.join(path, file)) == True:
        if file.find('.mp4') < 0:
            print(file)
        else:
            file_number = file.split('@')[0]
            print(file_number)
            new_file_name = '都挺好' + file_number + '.mp4'
            print(new_file_name)
            os.rename(os.path.join(path, file), os.path.join(new_path, new_file_name))

对于不规范的文件重命名,可以使用excel表格,通过对表格的处理来重命名,

猜你喜欢

转载自blog.51cto.com/m51cto/2375840
今日推荐