List All Text Files in a Folder

Easy ⏱ 8 min 77% acceptance ★★★★★ 4.7
Write text_files(folder) returning a sorted list of the file names (not full paths) in folder that end with .txt, case-insensitively (.TXT matches too). Use os.listdir.

Examples

Example 1
Input
text_files('docs')  # folder holds a.txt, b.PDF, c.TXT
Output
['a.txt', 'c.TXT']
Explanation

Both .txt variants match; the PDF does not.

Constraints

  • Case-insensitive extension check.
  • Sorted output.

Topics

File Handlingos.listdir

Companies

HCLTechAccenture

Hints

Hint 1

name.lower().endswith('.txt').

Hint 2

os.listdir gives names only, which is what we want.

Loading the Python runtime… Run executes your code and shows printed output; Submit checks your function against this problem's examples.