-1

I don't consider this question (How do I get the path and name of the file that is currently executing?) nor this one to be a duplicate of mine, because the answer to them is simply __file__. My question and the situation is more complicated and deserves a place to be independently googlable. I'm seeking the home dir of the path in which the script lies, it turns out (and this isn't even the only solution, perhaps, as that's a solution detail not a problem description), not just the path in which the script lies. Also, the context surrounding my question is very unique (running the script as root but wanting the home dir of another user's file system in which the script lies), and deserves special attention as others will encounter it too I'm sure.


I have a Python script located in /home/gabriel/dev/cpu_logger.py. Inside of it I am logging to /home/gabriel/cpu_log.log. I obtain the /home/gabriel part inside the script using pathlib.Path.home() as follows. I use that part as the directory of my log_file_path:

import pathlib

home_dir = str(pathlib.Path.home())
log_file_path = os.path.join(home_dir, 'cpu_log.log')

However, I now need to run the script as root to allow it to set some restricted file permissions, so I configured it to run as root at boot using crontab following these instructions here. Now, since it is running as root, home_dir above becomes /root and so log_file_path is /root/cpu_log.log. That's not what I want! I want it to log to /home/gabriel/dev/cpu_logger.py.

How can I do that?

I don't want to explicitly set that path, however, as I intend this script to be used by others, so it must not be hard-coded.

I thought about passing the username of the main user as the first argument to the program, and obtaining the home_dir of that user with os.path.expanduser("~" + username):

import os
import sys

username = sys.argv[1]
home_dir = os.path.expanduser("~" + username)

...but I don't want to pass an extra argument like this if I don't have to. How can I get the home dir as /home/gabriel even when this script is running under the root user?

Gabriel Staples
  • 36,492
  • 15
  • 194
  • 265
  • Or [this](https://stackoverflow.com/questions/3718657/how-do-you-properly-determine-the-current-script-directory)? – tink Dec 14 '22 at 00:27
  • @tink, I left a comment at the top of my question. The answer to those questions is `__file__`. My question is more complicated than that, and distinct. It requires more pieces. I'm seeking the home dir of the path in which the script lies, not just the path in which the script lies. – Gabriel Staples Dec 14 '22 at 00:29

1 Answers1

1

I figured it out!:

script_path_list = os.path.normpath(__file__).split(os.sep)
home_dir = os.path.join("/", script_path_list[1], script_path_list[2])

Explanation

__file__ contains the file path of the script, so we can split it by path separator (/, or os.sep) and read in the first couple components we need. Here's what I came up with. The first reference below was really useful.

import os

# Obtain the home dir of the user in whose home directory this script resides
script_path_list = os.path.normpath(__file__).split(os.sep)
home_dir = os.path.join("/", script_path_list[1], script_path_list[2])

# print the results
print("__file__         = {}".format(__file__))
print("script_path_list = {}".format(script_path_list))
print("home_dir         = {}".format(home_dir))

To help make sense of how this works, take a look at the printed output from above. You can see how the path got split and where the various path components ended up in the script_path_list:

__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

References

  1. Very useful: How to split a dos path into its components in Python
  2. what does the __file__ variable mean/do?
Gabriel Staples
  • 36,492
  • 15
  • 194
  • 265