1import os
2
3# List all files in a directory using scandir()
4basepath = 'my_directory/'
5with os.scandir(basepath) as entries:
6 for entry in entries:
7 if entry.is_file():
8 print(entry.name)
9
1from pathlib import Path
2
3basepath = Path('my_directory/')
4files_in_basepath = basepath.iterdir()
5for item in files_in_basepath:
6 if item.is_file():
7 print(item.name)
8