baoshiwei
2025-04-15 776a127b8db01cd4338f4db2a84ea567a65bff9f
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
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("处理完成!")