samedi 15 septembre 2018

How can python print file names in a directory and say they don't exist?

My program is supposed to get a directory and delete all the files in that directory. However, it says the file(s) are not found(they don't exist). I have included my full code(as it is not long at all) and the error output

Error Output:

Error404: CHESS_BASE_PLAYER.3dm not found!
Error404: dirty-metal-texture_1048-4784.jpg not found!
Error404: brushed_brass___seamless_texture_by_rls0812-d8wzhzn.jpg not found!
Error404: Tileable eroded scratch metal texture background 03.jpg not found!
Error404: Tileable eroded scratch metal texture background 03.jpg not found!

Code:

import os
import random

i = 0

while i < 5:
  file = random.choice(os.listdir("C:\\Users\\t\\Desktop\\test_delete")) #change dir name to whatever
  if os.path.exists(file):
    os.remove(file)
  else:
    print("Error404: " + file + " not found!")
    pass
    i += 1

print(file)

Thanks in advance!




Aucun commentaire:

Enregistrer un commentaire