I am working on a software installer for my current application. It needs to be installed to the System HDD. How owuld I detect the system drive and return the letter from Python?
Would the win32 extensions be useful? How about the os module pre packaged with Python?
Asked
Active
Viewed 8,778 times
3

Zac Brown
- 5,905
- 19
- 59
- 107
2 Answers
16
This is how to return the letter of the System drive on a Win32 platform:
import os
print os.getenv("SystemDrive")
The above snippet returns the system drive letter. In my case ( and most cases on windows) C:

Zac Brown
- 5,905
- 19
- 59
- 107
-
Note: this works only on NT platforms. Windows 9x didn't have this environment variable. – Paulo Freitas Aug 30 '13 at 01:24
2
If you install the win32 extensions, the following will get you the information you want:
In [82]: import win32api
In [83]: drives = win32api.GetLogicalDriveStrings()
In [84]: drives
Out[84]: 'C:\\\x00D:\\\x00E:\\\x00'
In [85]: drives.split('\x00')
Out[85]: ['C:\\', 'D:\\', 'E:\\', '']
Ignore the last item, due to a terminating character in the string returned by win32's GetLogicalDriveStrings function.

ars
- 120,335
- 23
- 147
- 134
-
thanks for the quick response, but that isn't what I need. That method returns every drive that is connected to the system. I just need the drive the OS is installed on. I figured out how to do and answered it below. – Zac Brown Oct 24 '10 at 01:08
-