Get the number of Python drives

Asked 2 years ago, Updated 2 years ago, 68 views

As a beginner at Python, I have a question.
How do I get the number of drives on that computer in Python? For example, some computers have drive C only, and some computers may have drive D or E. Is there a way to get the number or type of drive?

Windows 10 and Python 3.8 are currently in use.
Thank you.

python operating-system

2022-09-20 19:14

1 Answers

https://docs.microsoft.com/en-us/windows/win32/api/fileapi/nf-fileapi-getlogicaldrives

Must be obtained as a function of the above address.

Python provides the default module, ctypes, so you can invoke the native library immediately.

import ctypes
bitmask = ctypes.windll.kernel32.GetLogicalDrives()
print("{0:b}".format(bitmask))

1100

The GetLogicalDrives function returns a result with a bitmask and determines whether each bit digit exists.

The result of the above code is 1100, so it means that there are no A, B, C, D drives.


2022-09-20 19:14

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.