python 遍歷指定目錄下所有的文件進行文件名的修改


遍歷指定目錄下所有的文件進行文件名的修改

import os,re

class Ren:

    def __init__(self,file_dir,new_str):

        self.file_dir = file_dir
        self.new_str = new_str


    def file_name(self):
        L = []
        if os.path.exists(self.file_dir):
            for root, dirs, files in os.walk(self.file_dir):
                for file in files:
                    if os.path.splitext(file)[1] == '.success':
                        L.append(os.path.join(root, file))
        else:
            print('文件夾不存在!!')


    def ren_name(self):

        for file in self.file_name():
            try:
                new_str = re.compile('.success')
                str = new_str.sub('', file)
                os.rename(file,str)
                print('%s修改成功!!!'%file)
            except(FileExistsError):
                print('%s文件已存在' %str)
            continue

FILE_DIR = r'd:\1'
FILE_STR = ''

ren = Ren(FILE_DIR,FILE_STR)

ren.ren_name()

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM