文件名的批量修改
import os
class BatchRename():
def __init__(self):
self.path = 'D:/代码/article/python处理/文件名批量修改/修改标注-order/'
def rename(self):
filelist = os.listdir(self.path)
filelist.sort()
total_num = len(filelist)
i = 0
for item in filelist:
if item.endswith('.txt'):
src = os.path.join(os.path.abspath(self.path), item)
s = str(i)
s = s.zfill(2)
dst = os.path.join(os.path.abspath(self.path), s + '.txt')
try:
os.rename(src, dst)
print ('converting %s to %s ...' % (src, dst))
i = i + 1
except:
continue
print ('total %d to rename & converted %d txts' % (total_num, i))
if __name__ == '__main__':
demo = BatchRename()
demo.rename()