I'd like to go above and beyond since I need even more information. Here is how to obtain the:
- full path to the script being run
- directory of the script
- base filename of the script
- filename without the extension
- extension of the script
- home directory of the user running the script
- home directory of the user in whose home directory the script resides
From get_script_path.py in my eRCaGuy_hello_world repo:
import os
import sys
FULL_PATH_TO_SCRIPT = os.path.abspath(__file__)
SCRIPT_DIRECTORY = str(os.path.dirname(FULL_PATH_TO_SCRIPT))
SCRIPT_PARENT_DIRECTORY = str(os.path.dirname(SCRIPT_DIRECTORY))
# this also works, but its output will have the two dots (`/..`) in it.
SCRIPT_PARENT_DIRECTORY2 = f"{SCRIPT_DIRECTORY}/.."
FILENAME = str(os.path.basename(FULL_PATH_TO_SCRIPT))
FILENAME_NO_EXTENSION = os.path.splitext(FILENAME)[0]
FILENAME_EXTENSION = os.path.splitext(FILENAME)[1]
# Other useful paths:
# home directory of the current, running user
HOME_DIR_USER = os.path.expanduser("~")
# Obtain the home dir of the user in whose home directory this script resides,
# which may *not* be the home dir of the current user! Ex: run this script
# as root, via `sudo`, and you'll see that `HOME_DIR_USER` != `HOME_DIR_SCRIPT`.
script_path_list = os.path.normpath(FULL_PATH_TO_SCRIPT).split(os.sep)
HOME_DIR_SCRIPT = os.path.join("/", script_path_list[1], script_path_list[2])
# Bonus: add the parent directory to the start of the system PATH variable so
# that you can import modules directly in that directory above this script!
sys.path.insert(0, SCRIPT_PARENT_DIRECTORY)
# Now, assuming there is a `config.py`` file in the `SCRIPT_PARENT_DIRECTORY`,
# you can import it directly, like this:
#
# import config.py
#
# Print results
print(f"__file__: {__file__}")
print(f"FULL_PATH_TO_SCRIPT: {FULL_PATH_TO_SCRIPT}")
print(f"SCRIPT_DIRECTORY: {SCRIPT_DIRECTORY}")
print(f"SCRIPT_PARENT_DIRECTORY: {SCRIPT_PARENT_DIRECTORY}")
print(f"SCRIPT_PARENT_DIRECTORY2: {SCRIPT_PARENT_DIRECTORY2}")
print(f"FILENAME: {FILENAME}")
print(f"FILENAME_NO_EXTENSION: {FILENAME_NO_EXTENSION}")
print(f"FILENAME_EXTENSION: {FILENAME_EXTENSION}")
print("---")
print(f"HOME_DIR_USER: {HOME_DIR_USER}")
print(f"script_path_list: {script_path_list}")
print(f"HOME_DIR_SCRIPT: {HOME_DIR_SCRIPT}")
Example run and output, tested on Linux Ubuntu 20.04 and 22.04:
eRCaGuy_hello_world$ python/get_script_path.py
__file__: python/get_script_path.py
FULL_PATH_TO_SCRIPT: /home/gabriel/GS/dev/eRCaGuy_hello_world/python/get_script_path.py
SCRIPT_DIRECTORY: /home/gabriel/GS/dev/eRCaGuy_hello_world/python
SCRIPT_PARENT_DIRECTORY: /home/gabriel/GS/dev/eRCaGuy_hello_world
SCRIPT_PARENT_DIRECTORY2: /home/gabriel/GS/dev/eRCaGuy_hello_world/python/..
FILENAME: get_script_path.py
FILENAME_NO_EXTENSION: get_script_path
FILENAME_EXTENSION: .py
---
HOME_DIR_USER: /home/gabriel
script_path_list: ['', 'home', 'gabriel', 'GS', 'dev', 'eRCaGuy_hello_world', 'python', 'get_script_path.py']
HOME_DIR_SCRIPT: /home/gabriel
When run as root: notice that HOME_DIR_USER
, the active user's home dir, now changes:
eRCaGuy_hello_world$ sudo python/get_script_path.py
[sudo] password for gabriel:
__file__: python/get_script_path.py
FULL_PATH_TO_SCRIPT: /home/gabriel/GS/dev/eRCaGuy_hello_world/python/get_script_path.py
SCRIPT_DIRECTORY: /home/gabriel/GS/dev/eRCaGuy_hello_world/python
SCRIPT_PARENT_DIRECTORY: /home/gabriel/GS/dev/eRCaGuy_hello_world
SCRIPT_PARENT_DIRECTORY2: /home/gabriel/GS/dev/eRCaGuy_hello_world/python/..
FILENAME: get_script_path.py
FILENAME_NO_EXTENSION: get_script_path
FILENAME_EXTENSION: .py
---
HOME_DIR_USER: /root
script_path_list: ['', 'home', 'gabriel', 'GS', 'dev', 'eRCaGuy_hello_world', 'python', 'get_script_path.py']
HOME_DIR_SCRIPT: /home/gabriel
Additional explanation for finding HOME_DIR_SCRIPT
...which is the home directory of the path in which your Python script resides.
If the path to your script is /home/gabriel/GS/dev/eRCaGuy_dotfiles/useful_scripts/cpu_logger.py
, and you wish to obtain the home directory part of that path, which is /home/gabriel
, you can do this:
import os
# Obtain the home dir of the user in whose home directory this script resides
script_path_list = os.path.normpath(os.path.abspath(__file__)).split(os.sep)
home_dir = os.path.join("/", script_path_list[1], script_path_list[2])
To help make sense of this, here are the paths for os.path.abspath(__file__)
, script_path_list
, and home_dir
. Notice that script_path_list
is a list of the path components, with the first element being an empty string since it originally contained the /
root dir path separator for this Linux path:
os.path.abspath(__file__): /home/gabriel/GS/dev/eRCaGuy_dotfiles/useful_scripts/cpu_logger.py
script_path_list: ['', 'home', 'gabriel', 'GS', 'dev', 'eRCaGuy_dotfiles', 'useful_scripts', 'cpu_logger.py']
home_dir: /home/gabriel
My answer where I first documented this: Python: obtain the path to the home directory of the user in whose directory the script being run is located [duplicate]
See also
- My answer on how to do the same thing in Bash: How to obtain the full file path, full directory, and base filename of any script being run OR sourced... ...even when the called script is called from within another bash function or script, or when nested sourcing is being used!
- This related, useful answer to: How do I get the parent directory in Python?
__file__
attribute absolute or relative? – Julee