博文

Python 3 文件重命名(2012-08-02 17:36:00)

摘要: 一段 Python 3 编写的重命名脚本,仅用于文件,不包括文件夹,一般说来文件重命名更实用点儿,自己用自己的东西怎么都顺手,递归是好东西: import os
import re

def BatchRenameFile(string_path, string_old_name, string_new_name): # We need three parameters, the path contains all the files you want to rename, the regular expression strings for old and new names
    re_old_name = re.compile(string_old_name) # re object compiled for match the files whose name need to be changed
    string_contents = os.listdir(string_path) # List all files and sub folders in current folder
    for string_content in string_contents: # For every object
        if os.path.isdir(string_content): # If it's a folder, enter it and execute this method in it, when finish, go back to parent folder
            os.chdir(string_content)
            BatchRenameFi......

阅读全文(6179) | 评论:2 | 复制链接