Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 619
Bentley4 Avatar asked Aug 20 '12 16:08

Bentley4


People also ask

What is Python root folder?

Root Directory. Where you located python.exe is also the root of your Python installation (assuming you aren't using a virtual environment). In this folder, you will find files relating directly to Python and modules you have installed.


2 Answers

Try this:

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

On Linux this returns /

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

like image 104
user2743490 Avatar answered Sep 29 '22 21:09

user2743490


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') 
like image 21
jterrace Avatar answered Sep 29 '22 21:09

jterrace