title: sorting images in 1 folder according to its labels in yaml file
body: <p>I have a yaml file which has filename and label and looks like below:</p>
  <pre><code>- 1003520587_a35cd70a-22ab-4f89-92f8-e44884d6225d_boolean: no-checkbox
  - 1003522794_3f24dcc8-60f1-4959-b0f4-d00a9f723a46_boolean: filled
  - 1003522794_3f24dcc8-60f1-4959-b0f4-d00a9f723a46_boolean: empty
  </code></pre>
  <p>And I have all images in 1 folder. I would like to separate each image according to its label respectively into its folder using python. How would I do it?</p>
  <p>I am getting file doesn't exists error using following code.</p>
  <pre><code>import yaml
  import pandas as pd
  import shutil
  # base folders / template destinations
  from_folder =  r'/home/user/Downloads/by_field/abschluss/'
  to_folder_base = r'home/user/Downloads/sorted_image/abschluss'
  
  with open('/home/user/Downloads/Yaml/abschluss.yaml', 'r') as stream:
          parsed_yaml=yaml.safe_load(stream)
          for i in parsed_yaml:
              for key in i:
                  img_name = key + &quot;.png&quot;
                  keyword = i[key]
                  to_folder = os.path.join(to_folder_base, keyword)
                  os.makedirs(to_folder, exist_ok=True)
                  old_img_path = os.path.join(from_folder, img_name)
                  new_img_path = os.path.join(to_folder, img_name)
                  shutil.move(old_img_path, new_img_path)
  </code></pre>
  <blockquote>
  <p>FileNotFoundError: [Errno 2] No such file or directory</p>
  </blockquote>
  <p>But when I checked using same path the file do exists</p>
  <p>I tried by converting yaml to csv file as well using pandas which looks like below:
  <a href="https://i.stack.imgur.com/tERTM.png" rel="nofollow noreferrer">attached image</a></p>
  <pre><code>import pandas as pd
  # base folders / template destinations
  from_folder = &quot;/home/user/Downloads/images/Leibteil/&quot;
  to_folder_base = &quot;/home/user/Downloads/sorted_image/&quot;
  
  # read in CSV file with pandas
  meta_ham = (pd.read_csv('/home/user/Downloads/csv/Leibteil.csv'))
  print(meta_ham)
  # iterate through each row of csv
  for index in range(len(meta_ham)):
      print(index)
      # get image name and corresponding group
      img_name = meta_ham[index] + &quot;.png&quot;
      print(img_name)
      keyword = meta_ham[label]
      print(keyword)
      # make a folder for this group, if it doesn't already exist. 
      # as long as exist_ok is True, then makedirs will do nothing if it already exists
      to_folder = os.path.join(to_folder_base, 'label')
      print(to_folder)
      os.makedirs(to_folder, exist_ok=True)
      # move the image from its original location to this folder
      old_img_path = os.path.join(from_folder, img_name)
      new_img_path = os.path.join(to_folder, img_name)
      shutil.move(old_img_path, new_img_path)
  </code></pre>
  <p>But using csv or pandas it throws <code>Keyerror: 0</code></p>