os模块文件名替换
代码
在数据清洗过程中,经常能看到问题名字的数据,导致训练的时候出错,这个时候就需要对这种问题数据做一下处理
import os
import re
src_img = 'D:/shuichi_test/img/'
src_txt = 'D:/shuichi_test/yolo/txt/'
def replace_file(path, suffix='.jpg'):
for file in os.listdir(path):
if ')' in file:
old_file = os.path.join(path, file)
# 以最后一个后缀点字符分割字符串
filename = os.path.splitext(file)[0]
filename = filename.replace(')', '')
new_file = os.path.join(path + filename + suffix)
os.rename(old_file, new_file)
def JPG_convert_jpg(path, suffix='.jpg'):
for file in os.listdir(path):
if file.endswith('.JPG'):
old_file = os.path.join(path, file)
filename = os.path.splitext(file)[0]
new_file = os.path.join(path + filename + suffix)
os.rename(old_file, new_file)