开发者

get the windows installation drive using python

How to detect the wi开发者_JS百科ndows installation path or drive using python code ?


>>> import os
>>> os.environ['SYSTEMDRIVE']
'C:'


You can use GetWindowsDirectory via the ctypes library to get the location of the Windows folder, and then you can use os.path.splitdrive to get the drive letter. For example:

import ctypes
import os

kernel32 = ctypes.windll.kernel32
windows_directory = ctypes.create_unicode_buffer(1024)
if kernel32.GetWindowsDirectoryW(windows_directory, 1024) == 0:
    # Handle error
else:
    windows_drive = os.path.splitdrive(windows_directory)[0]


You can use the WINDIR enviroment variable.

os.environ['WINDIR']


Use this code to just get the letter, and nothing else:

import os
os.environ['WINDIR'].split(":\\")[0]

Example output:

>>> os.environ['WINDIR'].split(":\\")[0]

'C'

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜