0

I wrote the code and now I have problem that the code finde only the first value (for example only img, we have img2 too, but it went to another picture) and copy only one, but we have 2 possibilities.

for i in df_list:
    img = (filepath + i + ".jpg")
    img2 = (filepath + i + "-1" + ".jpg")
    img3 = (filepath + i + "-2" ".jpg")
    img4 = (filepath + i + "-3" + ".jpg")
    img5 = (filepath + i + "-4" + ".jpg")
    img6 = (filepath + i + " -5" + ".jpg")
    try:
        shutil.copy(img, newpath, follow_symlinks=True)

    except:
        try:
            shutil.copy(img6, newpath, follow_symlinks=True)

        except:
            try:
                shutil.copy(img2, newpath, follow_symlinks=True)

            except:
                  try:
                    shutil.copy(img3, newpath, follow_symlinks=True)

                  except:
                        try:
                          shutil.copy(img4, newpath, follow_symlinks=True)

                        except:
                                try:
                                    shutil.copy(img5, newpath, follow_symlinks=True)

                                except:
                                    with open("C:/Users/"+user+"/Desktop/J/"+datum+"/"+"Napake.txt", "a") as text_file:
                                      print("Slika za ident {} ne obstaja.\n".format(i), file=text_file)

I neeed help, thank you for answers.

deadshot
  • 8,881
  • 4
  • 20
  • 39
Matija
  • 73
  • 5

1 Answers1

1

Rather than copying the try,except you can loop over the filenumbers and try copy each file. and print an error if there is an exception.

save_path = "C:/Users/" + user + "/Desktop/J/" + datum + "/" + "Napake.txt"
for folder in df_list:
    for index in range(6):
        if index == 0:
            img = filepath + folder + ".jpg"
        else:
            img = f"{filepath}{folder}-{index}.jpg"

        try:
            shutil.copy(img6, newpath, follow_symlinks=True)
            with open(save_path, "a") as text_file:
                text_file.write(f"Slika za ident {folder}-{i} ne obstaja.\n")
        except Exception as e:
            print('could not copy file')
            print(e)

I would also recommend having a look at this answer to see how to copy all files in a directory.

James Burgess
  • 487
  • 1
  • 4
  • 12