0

I have a variable in list format in which there are number of paths. I want to access each path 1 by 1 to write my output on that path

I have created a list which will have all the paths

    folders = glob(test_img_path)
    print(folders)

###############OUTPUT of FOLDERS Variable################

['C:\\Python35\\target_non_target\\Target_images_new\\video_tiger_23sec', 'C:\\Python35\\target_non_target\\Target_images_new\\video_tiger_leopard']

####################END#################################

##############LINE ON WHICH THE PATH WILL BE USED TO WRITE OUTPUT######

    cv2.imwrite(folders+"\\{}.jpg".format(img_name),image)

###########END############

There will be many paths in this folders variable. How can i modify my code to access these paths 1 by 1 from this list to write my final output on that path

Ankit
  • 203
  • 3
  • 14
  • Just put that statement in a for loop iterating over `folders`. – Jan Apr 14 '19 at 05:47
  • @Jan for k in folders: cv2.imwrite(folders[k]+"\\{}.jpg".format(img_name),image) this throws an error. TypeError: list indices must be integers or slices, not str – Ankit Apr 14 '19 at 05:50

1 Answers1

1

Simply loop through the list and write to the path

folders = glob(test_img_path)
for folder in folders:
    image = cv2.imread(...)
    ...
    cv2.imwrite(folder + "\\{}.jpg".format(img_name), image)


M.Yousuf
  • 53
  • 7
  • @ M Yousuf This script writes the data only to the last folder in the loop – Ankit Apr 14 '19 at 05:56
  • Have you indented the code correctly? Because that's not possible. I guess you have written the cv2.imwrite line outside the for loop – M.Yousuf Apr 14 '19 at 06:00
  • @M Yousuf Sorry brother. It was error on my end about the indentation. Your suggestion works completely fine. But the images of folder 1 get repeated while writing the images of folder2. I guess another indentation issue but not able to figure out. Can i share my complete code script with u if u r ok with it? – Ankit Apr 14 '19 at 06:07