-1

how would i check if all files in specific directory dir/ share the same file ending .txt.

This is how I find the files:

for dirpath, dirnames, files in os.walk(dir):
    print(files)
John Bielowski
  • 199
  • 2
  • 11
  • 4
    Hint: use the `all` function, the `endswith` method, and a generator expression. – chepner May 31 '20 at 13:50
  • You can use a not to invert the check in [one of these answer](https://stackoverflow.com/questions/3964681/find-all-files-in-a-directory-with-extension-txt-in-python) – DarrylG May 31 '20 at 13:52
  • Addition to chepner's hints, you might want to use `str.lower` to cover an extension like `.tXt`. – Mustafa Aydın May 31 '20 at 13:57

1 Answers1

1
all_end_with_txt = True
for dirpath, dirnames, files in os.walk(dir):
        for file in files:
                if not file.lower().endswith('.txt'):
                        all_end_with_txt = False
Conor
  • 691
  • 5
  • 14