问题描述:
Traceback (most recent call last):
File "E:/Tool_code/rename.py", line 72, in
newname.rename()
File "E:/Tool_code/rename.py", line 57, in rename
os.rename(src, dst)
FileExistsError: [WinError 183] 当文件已存在时,无法创建该文件。: 'F:\\Data_set\\Picking_point\\0001306.jpg' -> 'F:\\Data_set\\Picking_point\\000131.jpg'
我的重命名代码如下
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import os
class ImageRename():
def __init__(self):
# self.path = 'E:/sugarcane/JPEGImages1/'
self.path='F:/Data_set/Picking_point/'
def rename(self):
filelist = os.listdir(self.path)
total_num = len(filelist)
# print(total_num)
i = 0
for item in filelist:
if item.endswith('.jpg') or item.endswith('.JPG'):
src = os.path.join(os.path.abspath(self.path), item)
dst = os.path.join(os.path.abspath(self.path), '000' + format(str(i), '0>3s') + '.jpg')
os.rename(src, dst)
# print ('converting %s to %s ...') % (src, dst)
i = i + 1
if __name__ == '__main__':
newname = ImageRename()
newname.rename()
2解决办法
将所有的文件用右键重命名为0,然后用重命名排序代码进行重命名排序工作。