python 同时打开两个文件,共同处理

比如有两个标签文件,一个txt里放着所有图像的坐标,每行一个图像,另一个txt里放着图片的类别标签

关键语句:

with open ( flie1 ) as f1:

    with open ( file2 ) as f2:

           for line1,lin2 in zip ( f1, f2):

                  pa = line1.split()

                  pb = line2.split()

                 ……

#load file and process
root = '/home/dan/Downloads/hand_pose_action-master/'
person_id = 'Subject_6' #(1-6)
#to each people
labelroot  =  root+'Hand_pose_annotation_v1/'+ person_id+'/'
imgroot = root+'Video_files/'+ person_id+'/'
list = os.listdir(labelroot)

for ilist in list:
    path = os.path.join(labelroot,ilist)
    print('object path:  ',path)
    videolist = os.listdir(path)
    for jvideo in videolist:
        videopath = os.path.join(path, jvideo)
        labelfile = videopath + '/skeleton.txt'
        graspfile = videopath + '/grasp.txt'
        #open two file meanwhile
        with open(labelfile) as fp1:
            with open(graspfile) as fp2:
                for la,gr in zip(fp1,fp2):
                    partaaa = la.split()
                    partbbb = gr.split()
                    gsp = partbbb[1]
                    pose = partaaa[1]

 

你可能感兴趣的:(coding)