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.
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.
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')
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With