os.listdrives

All three are Windows only and new in Python 3.12 - they do not exist on Linux or macOS. listdrives does not test whether a drive is ready or accessible; listmounts needs a volume GUID path from listvolumes, not a drive letter.

os functionPython 3.12+
Common call
os.listdrives()
Returns
['C:\\', 'D:\\'] on a typical machine
Replaces
Looping over A: to Z: with os.path.exists, wmic / PowerShell calls
Watch out
Windows only (3.12+): guard with hasattr in portable code
os.listdrives() / os.listvolumes() / os.listmounts(volume)
→ list[str]

Parameters

NameTypeRequiredDescription
volumestryeslistmounts: a volume GUID path as returned by listvolumes(). Anything else, e.g. 'C:\\', raises OSError.

Return value

list[str] — listdrives: drive roots like 'C:\\'; listvolumes: GUID paths like '\\\\?\\Volume{...}\\'; listmounts: the mount points of one volume (possibly empty).

Common patterns

Drives that are actually usable
listdrives includes empty card readers and disconnected network drives; test each one.
import os
ready = [d for d in os.listdrives() if os.path.exists(d)]
Volumes and where they are mounted
A volume can be mounted at several places or nowhere (empty list).
import os
for vol in os.listvolumes():
    print(vol, os.listmounts(vol))
Portable "roots" list
On POSIX there is a single root.
import os
roots = os.listdrives() if hasattr(os, 'listdrives') else ['/']

Examples

1. Guarded call, same result type everywhere
import os drives = os.listdrives() if hasattr(os, 'listdrives') else [] isinstance(drives, list)
Returns
True
2. Split the drive off a Windows path
import ntpath ntpath.splitdrive(r'C:\Users\ada')
Returns
('C:', '\\Users\\ada')
3. Drive, root and rest (3.12+)
import ntpath ntpath.splitroot(r'C:\Users')
Returns
('C:', '\\', 'Users')
4. pathlib sees the drive too
from pathlib import PureWindowsPath p = PureWindowsPath(r'D:\photos\cat.jpg') (p.drive, p.anchor)
Returns
('D:', 'D:\\')
5. Recognise a volume GUID path
v = r'\\?\Volume{0a1b2c3d-0000-0000-0000-100000000000}' + '\\' (v.startswith(r'\\?\Volume{'), v.endswith('\\'))
Returns
(True, True)

Pitfalls

1. Dropping the backslash from a drive root
listdrives returns 'C:\\' with the root backslash. 'C:' alone means the current directory ON drive C, so joining to it gives a drive-relative path.
'C:'
import ntpath
ntpath.join('C:', 'Users')
'C:Users'
'C:\\'
import ntpath
ntpath.join('C:' + '\\', 'Users')
'C:\\Users'
2. Looking up a drive without its root
The list holds roots, so a bare letter with colon is not found.
'C:' in drives
drives = ['C:\\', 'D:\\']
'C:' in drives
False
'C:\\' in drives
drives = ['C:\\', 'D:\\']
'C:' + '\\' in drives
True

When to use

Use it
  • Drive pickers, backup and disk tools on Windows
  • Finding volumes that have no drive letter (listvolumes + listmounts)
Reach for something else
  • Linux / macOS mounts → read /proc/mounts or use psutil.disk_partitions (third party)
  • Free space per drive → shutil.disk_usage(drive)

Notes

CPython impl
Thin wrappers over the Win32 drive and volume enumeration APIs in Modules/posixmodule.c (compiled only on Windows)
Availability
Windows only, added in 3.12 (all three). Each raises an auditing event (os.listdrives, os.listvolumes, os.listmounts)
Observed
On a Windows 11 machine with Python 3.13: listdrives() gave ['C:\\', 'D:\\']; listvolumes() gave four '\\\\?\\Volume{...}\\' paths; listmounts of two of them gave ['C:\\'] and ['D:\\'], the other two []; listmounts('C:\\') raised OSError (WinError 87)

FAQ

listdrives, listvolumes and listmounts are Windows only and were added in Python 3.12, so they raise AttributeError on Linux, macOS and older Pythons. The examples on this page therefore guard with hasattr or work with ntpath / PureWindowsPath, which behave the same everywhere.