python3批量为文件重命名

简介

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

代码:shell

#!/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表格,经过对表格的处理来重命名,ide

相关文章
相关标签/搜索