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

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


Try this:

import osdef root_path():    return os.path.abspath(os.sep)

On Linux this returns /

On Windows this returns C:\\ or whatever the current drive is


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')


Here's what you need:

import sys, osdef get_sys_exec_root_or_drive():    path = sys.executable    while os.path.split(path)[1]:        path = os.path.split(path)[0]    return path