import os def process_file(file_path): with open(file_path, 'r') as file: lines = file.readlines() with open(file_path, 'w') as file: for line in lines: # 尝试从每一行中提取第一个数字 try: number = int(line.split()[0]) except ValueError: # 如果提取失败,跳过当前行 file.write(line) continue new_number = number + 1 # 替换原始行中的第一个数字 updated_line = line.replace(str(number), str(new_number), 1) # 写入更新后的行 file.write(updated_line) if __name__ == "__main__": # 指定目录 target_directory = 'E:\\herb_scan.v1i.yolov8\\valid\\labels' # 获取目录下的所有txt文件 txt_files = [file for file in os.listdir(target_directory) if file.endswith('.txt')] # 处理每个文件 for txt_file in txt_files: file_path = os.path.join(target_directory, txt_file) process_file(file_path) print("处理完成!")