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

6

57

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.

Schottische answered 20/8, 2012 at 16:32 Comment(3)
Well on linux, the root directory is not necessarily on the same drive python is installed on. It depends on how the system is partioned (single drive or multi-drive).Louvar
you can use executable, prefix or exec_prefix from sys (docs.python.org/py3k/library/sys.html)Wrench
None of these answers work for me. How can I join root_dir="C:/Users/folder" with filename="data/file1.txt" to get an absolute path? In not-windows it's easy with just os.path.join(root_dir, filename), but I can't find a solution in Windows.Fondness
C
28

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')
Coatbridge answered 20/8, 2012 at 16:38 Comment(4)
It would've been nice if os.path.splitdrive(sys.executable)[0] would also return / as root in linux. But it's good enough, thank you!Schottische
@Schottische I think they did it that way for correctness. / really isn't a drive letterCoatbridge
True, but I was thinking maybe there was some python object in one of the standard library modules called root which always returned the root. But it seems there isn't.Schottische
When doing a similar task, I was partial to using file to get the location of the script instead of using sys.executable.Middleweight
E
126

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

Electromechanical answered 7/3, 2014 at 16:26 Comment(2)
This will give you the letter of the drive you are running your script from on Windows; not the drive letter the python executable is running from as the accepted answer provides. Either could be what various users who find this page want, the accepted answer provides what the original question was asking for.Fenn
This runs well on my mac tooYann
C
28

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')
Coatbridge answered 20/8, 2012 at 16:38 Comment(4)
It would've been nice if os.path.splitdrive(sys.executable)[0] would also return / as root in linux. But it's good enough, thank you!Schottische
@Schottische I think they did it that way for correctness. / really isn't a drive letterCoatbridge
True, but I was thinking maybe there was some python object in one of the standard library modules called root which always returned the root. But it seems there isn't.Schottische
When doing a similar task, I was partial to using file to get the location of the script instead of using sys.executable.Middleweight
I
8

Here's what you need:

import sys, os

def get_sys_exec_root_or_drive():
    path = sys.executable
    while os.path.split(path)[1]:
        path = os.path.split(path)[0]
    return path
Interrupted answered 20/8, 2012 at 16:48 Comment(1)
IMO, this answer has merit on the points argued about in comments of other answers. It's rather self-obvious which drive this is returning the root of - it's the drive of the python executable. If someone needed a different drive, they should start with a different path. Without that knowledge, this answer uses the superior assumption.Dolabriform
S
5

Using pathlib (Python 3.4+):

import sys
from pathlib import Path

path = Path(sys.executable)
root_or_drive = path.root or path.drive
Shawndashawnee answered 25/5, 2017 at 21:41 Comment(3)
Perhaps use the ternary operator on that last lineEndarch
If you are able to use pathlib >= 3.5, then a wonderful method would be to use Path.home() which is OS dependent and can be used to as root for commands to interact with.Treatment
On Windows with pathlib2 (Python 2.7 backport of Python 3.5+ pathlib), this doesn't give the right answer as the root attribute returns '\\', so the correct tanswer would seem to be path.drive + path.root as path.drive returns empty '' on Linux.Reputable
C
5

Based on the answer by Eugene Yarmash, you can use the PurePath.anchor property in pathlib as early as Python >= 3.4, which is:

The concatenation of the drive and root

Using sys.executable to get the location of your python installation, a complete solution would be:

import sys
from pathlib import Path

root = Path(sys.executable).anchor

This results in '/' on POSIX (Linux, Mac OS) and should give you 'c:\\' on Windows (assuming your installation is on c:). You can use any other path instead of sys.executable to get the drive and root where this other path is located.

Criminate answered 31/1, 2021 at 2:9 Comment(0)
N
0

Here's a cross platform, PY2/3 compatible function that returns the root for a given path. Based on your context, you can feed the python executable path into it, the path where the script resides, or whatever makes sense for your use case.

import os

def rootpath( path ):     
    return os.path.splitdrive(os.path.abspath( path ))[0] + os.sep
    

So for the root path of the Python interpreter:

import sys

PY_ROOT_PATH = rootpath( sys.executable )
Niacin answered 27/2, 2021 at 16:4 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.