Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

get the windows installation drive using python

Tags:

python

window

How to detect the windows installation path or drive using python code ?

like image 614
Giri Dhar Avatar asked Feb 25 '26 07:02

Giri Dhar


2 Answers

>>> import os
>>> os.environ['SYSTEMDRIVE']
'C:'
like image 75
poke Avatar answered Feb 26 '26 20:02

poke


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]
like image 43
Adam Rosenfield Avatar answered Feb 26 '26 21:02

Adam Rosenfield