Python Glossary Русский

Python 3.12+Windowsosfunction

os.listdrives()

file system

Windows only: returns a list of drive names on the system (like C:\ or D:\ ); the function does not test drives for accessibility. Python 3.12+.

Syntax

os.listdrives()

The list includes every drive letter, even with no media inserted or an unreachable network drive — accessing such a drive may hang or raise. Check each drive separately to see whether it is ready. Unix has no counterpart: there the whole tree starts at a single root.

Examples

import os, sys
print(hasattr(os, 'listdrives') == (os.name == 'nt' and sys.version_info >= (3, 12)))  # → True (только Windows, 3.12+)
drives = os.listdrives() if hasattr(os, 'listdrives') else []
print(type(drives).__name__)  # → list
print(all(isinstance(d, str) for d in drives))  # → True
print(len(drives) > 0 or os.name != 'nt')  # → True (на Windows всегда есть хотя бы системный диск)

See also

Common errors

Open in the full glossarydocs.python.orgReport an inaccuracy