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.
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')
os.path.splitdrive(sys.executable)[0]
would also return /
as root in linux. But it's good enough, thank you! –
Schottische /
really isn't a drive letter –
Coatbridge 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
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')
os.path.splitdrive(sys.executable)[0]
would also return /
as root in linux. But it's good enough, thank you! –
Schottische /
really isn't a drive letter –
Coatbridge 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
path
. Without that knowledge, this answer uses the superior assumption. –
Dolabriform Using pathlib
(Python 3.4+):
import sys
from pathlib import Path
path = Path(sys.executable)
root_or_drive = path.root or path.drive
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 root
attribute returns '\\'
, so the correct tanswer would seem to be path.drive + path.root
as path.drive
returns empty ''
on Linux. –
Reputable 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.
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 )
© 2022 - 2024 — McMap. All rights reserved.
root_dir="C:/Users/folder"
withfilename="data/file1.txt"
to get an absolute path? In not-windows it's easy with justos.path.join(root_dir, filename)
, but I can't find a solution in Windows. – Fondness