7/24/2021

find all txt file in dir and read line by line in txt. python example source code

 

python code

.

import glob

files = []
start_dir = '/MareArts/dev/dataset/coco_yolov5pytorch/train/labels/'
# output_path = '/Volumes/output/'
pattern = "*.txt"
search_dir = os.path.join(start_dir,pattern)

total = 0
txt_list = glob.glob(search_dir)
for i,v in enumerate(txt_list):
print(i,v)
file1 = open(v, 'r')
Lines = file1.readlines()
count = 0
# Strips the newline character
for line in Lines:
count += 1
print("Line{}: {}".format(count, line.strip()))

.


result

0 /MareArts/dev/dataset/coco_yolov5pytorch/train/labels/000000253429_jpg.rf.7f12980ae02c096d1b9ac0b574d3ac1b.txt
Line1: 10 0.6768149882903981 0.2203125 0.6463700234192038 0.196875
Line2: 36 0.32786885245901637 0.67265625 0.6557377049180327 0.6359375
Line3: 48 0.34543325526932084 0.57578125 0.36533957845433257 0.6765625
1 /MareArts/dev/dataset/coco_yolov5pytorch/train/labels/000000009465_jpg.rf.3d5a6b94f8c1afdc004f2641cd578912.txt
Line1: 36 0.6703125 0.8002309468822171 0.2109375 0.22170900692840648
Line2: 73 0.06015625 0.684757505773672 0.1171875 0.42494226327944573
Line3: 48 0.16484375 0.7621247113163973 0.04375 0.16166281755196305
Line4: 48 0.24921875 0.7829099307159353 0.0296875 0.16628175519630484
Line5: 48 0.2234375 0.7736720554272517 0.0359375 0.17090069284064666
Line6: 48 0.19765625 0.76905311778291 0.03125 0.16628175519630484
Line7: 48 0.1265625 0.7840646651270208 0.075 0.20554272517321015
2 /MareArts/dev/dataset/coco_yolov5pytorch/train/labels/000000009466_jpg.rf.5b49bdd7c037ed299dd3b6fd2531c34d.txt
Line1: 20 0.2625 0.45433255269320844 0.3078125 0.4496487119437939
3 /MareArts/dev/dataset/coco_yolov5pytorch/train/labels/000000009469_jpg.rf.38d5d254d972459fba2431d6ed2baffe.txt
Line1: 27 0.262 0.7878048780487805 0.314 0.3853658536585366
Line2: 48 0.304 0.5146341463414634 0.282 0.9170731707317074
4 /MareArts/dev/dataset/coco_yolov5pytorch/train/labels/000000009474_jpg.rf.fe5b0057cd113427b333ccd60c2d10d5.txt
Line1: 10 0.3203125 0.5530660377358491 0.2953125 0.8702830188679245
5 /MareArts/dev/dataset/coco_yolov5pytorch/train/labels/000000009478_jpg.rf.1584a7148b62cc3612da3407115b14b7.txt
Line1: 33 0.52421875 0.5612745098039216 0.21875 0.44607843137254904
...
...


Thank you.
https://study.marearts.com

No comments:

Post a Comment