Batch renaming of python files

Code meaning: rename all files under
the path , and the result of the renaming is that the previous name is added before the namepathpngzs_
insert image description here

import os

def rename(path):
        filelist = os.listdir(path)
        for item in filelist:
            if item.endswith('.png'):
                src = os.path.join(os.path.abspath(path), item)
                dst = os.path.join(os.path.abspath(path), 'zs_' + item)
                try:
                    os.rename(src, dst)
                except:
                    continue

rename(path = "./label/")

Guess you like

Origin blog.csdn.net/weixin_44669966/article/details/125905958