Python Glossary Русский

all Python 3osfunction

os.listdir()

directories

Returns the list of file and folder names in the given directory (unsorted). The current directory by default.

Syntax

os.listdir(path='.') -> list[str]

The order is arbitrary and depends on the filesystem — wrap the call in sorted() for stable output. The special '.' and '..' entries are not included. Only bare names come back, so open a file via os.path.join(path, name). If you also need each entry's type, os.scandir is faster because it avoids a separate stat per name. A bytes path yields bytes names.

Examples

import os
files = os.listdir('.')  # → ['file.py', 'data.csv', ...]
py_files = [f for f in os.listdir('.') if f.endswith('.py')]  # → все .py файлы
print(os.listdir('/tmp'))  # → содержимое /tmp
print(len(os.listdir('.')))  # → кол-во объектов в директории
sorted_files = sorted(os.listdir('.'))  # → отсортированный список

See also

Common errors

Open in the full glossarydocs.python.orgReport an inaccuracy