MattyK
MattyK

Reputation: 31

Python, Find the drive letter with windows installed

I started making a program for fun when I encountered a problem. The problem was that I wanted to find the drive letter with windows installed on it (root drive). I assumed there was a function already made for that but I searched for a while and could not find one.

I wrote this code to do what I just described. Is this code redundant and am I being an idiot? There is probably a much easier way...

def root():

root = ""
i = 0
drives = win32api.GetLogicalDriveStrings()
drives = drives.split("\000")[:-1]

for i in range(0, len(drives)):
    drives[i] = drives[i].replace("\\", "/")

i = 0
for i in range(0, len(drives)):

    if os.path.exists(drives[i] + "Windows"):
        root = drives[i]
        break

return root

I suppose someone can use this for testing purposes or what not.

Upvotes: 1

Views: 1498

Answers (1)

Humbalan
Humbalan

Reputation: 717

There is an environment variable windir. On my computer in contains "C:\Windows". You can read this variable by os.getenv('WINDIR') (refer to python 2:os.getenv() or python 3:os.getenv()).

Upvotes: 3

Related Questions