Find System Hard Disk Drive from Python?

点点圈 提交于 2019-11-28 10:08:37

问题


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?


回答1:


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:




回答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.



来源:https://stackoverflow.com/questions/4006730/find-system-hard-disk-drive-from-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!