When a program needs the entries in a directory, os.listdir returns a list of strings containing their names. It includes files, subdirectories, and hidden files, but not the directory path. section 5.20
List a directory
filenames = os.listdir('/etc/') Use os.path.join to combine the directory with each returned filename before opening or processing it; this keeps path separators cross-platform. section 5.20 section 10.12
Choose a different filesystem tool when the basic listing is not enough:
- Use glob.glob when a filename pattern such as *.conf should filter entries and produce complete paths. section 5.20
- Use pathlib when an object-oriented filesystem API is preferable, or os.walk when processing files recursively. section 5.20 section 10.12
- Use os.path.splitext to extract file extensions from the names returned by os.listdir. section 4.22
filenames = os.listdir('/etc/')
A minimal directory-listing expression demonstrates the function's input and list-of-filenames result. section 5.20


