Python批量重命名文件夹中图像文件的完整实现
本文介绍如何使用Python脚本对指定文件夹内的图像文件进行连续重命名。通过遍历目录并提取JPG格式文件,程序自动将文件按顺序重命名为从101开始的连续数字。该方法适用于需要整理大量图像资源的场景,代码简洁且易于扩展。
以下介绍一种使用Python实现文件夹内图像连续重命名的具体方法。
import os
class BatchRename():
def __init__(self):
self.path = 'C:/Users/zxl/Desktop/tr'
def rename(self):
filelist = os.listdir(self.path)
total_num = len(filelist)
i = 101
for item in filelist:
if item.endswith('.jpg'):
src = os.path.join(os.path.abspath(self.path), item)
dst = os.path.join(os.path.abspath(self.path), str(i) + '.jpg')
try:
os.rename(src, dst)
print 'converting %s to %s ...' % (src, dst)
i = i + 1
except:
continue
print 'total %d to rename & converted %d jpgs' % (total_num, i)
if __name__ == '__main__':
demo = BatchRename()
demo.rename()
上述内容即为Python实现文件夹内图像连续重命名的完整方案,供读者参考与使用。
本站声明
本文内容仅供参考,不代表本站观点。转载或引用请注明出处。如发现内容问题,请通过联系我们页面反馈。













