One - One Code All

Blog Content

python 批量修改文件名

Python   2011-03-05 13:03:05
import os

class BatchRename():
    def __init__(self):
        self.path = 'D:/tmp/a'

    def print1(self):
        filelist = os.listdir(self.path)
        total_num = len(filelist)
        i = 1
        for item in filelist:
            #print(item)
            src = os.path.join(os.path.abspath(self.path), item)
            newname = item
            newname = str(i) + "_" + item
            newname = newname.replace("pingshu.com","")
            dst = os.path.join(os.path.abspath(self.path), '单田芳_童林传_' + item)
            print(dst)

            os.rename(src, dst)
            i = i + 1

if __name__ == '__main__':
    demo = BatchRename()
    demo.print1()


将该文件放在目标目录文件夹外面,执行,文件名会批量修改。


上一篇:python的dataframe重设索引reset_index(drop=True)
下一篇:python的dataframe重建索引reset_index(drop=True)

The minute you think of giving up, think of the reason why you held on so long.