dimanche 2 mai 2021

Create list of files names of certain file types, if folder does not contain file type, print this statement

I am creating a list of files names from files of a certain type in a folder:

items = os.listdir(r'E:/folder/test')

print('checking directory...')
print('your files are: ')
print()
filenames = []
for names in items:
    if names.endswith(".shp"):
        filenames.append(names)
        print(filenames)
        print()
    else:
        print("there are no files ending with .shp in your folder")

If there are no files in the folder that are .shp files, I want the if/else conditional to print a statement saying this. My code is currently doing both, any solution? Thanks!

Aucun commentaire:

Enregistrer un commentaire