Skip to content
Advertisement

A system independent way using python to get the root directory/drive on which python is installed

For Linux this would give me /, for Windows on the C drive that would give me C:\. Note that python is not necessarily installed on the C drive on windows.

Advertisement

Answer

You can get the path to the Python executable using sys.executable:

>>> import sys
>>> import os
>>> sys.executable
'/usr/bin/python'

Then, for Windows, the drive letter will be the first part of splitdrive:

>>> os.path.splitdrive(sys.executable)
('', '/usr/bin/python')
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement