简体   繁体   中英

Find System Hard Disk Drive from Python?

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?

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:

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM