import os sourcepath = '/home/xiongweixp/data/handwriting/train/' with open(sourcepath+'val_list.txt') as f: all_lines = f.readlines() for line in all_lines: # print(line) elements = line.split(' ') dir_name = elements[1].strip() file_name = elements[0] pic_name = file_name.split('/')[1] targetPath = '/home/xiongweixp/data/handwriting/pytorch_data/validate/'+dir_name if not os.path.exists(targetPath): os.makedirs(targetPath) cmd = 'cp '+sourcepath+file_name+' '+targetPath+'/'+pic_name print(cmd) os.system(cmd)