I've recently began using ConfigParser() for my python scripts to add some functionality to them for config files. I know how to use it but I have a problem. My script needs to run as the root user, using sudo. The config files are in ~/.config/scriptconfig/
but when you run a script as sudo it temporarily changes users to root, so it doesn't find the config files. What I want to do is get the config file of the effective user so it grabs /home/myuser/.config/scriptconfig/config.cfg
instead of /root/.config/scriptconfig/config.cfg
, which doesn't exist.
I need the script to be able to run on different machines, not just mine. So I need to get the home directory of the effective user
Here is an example of the code I'm trying to use:
import os, ConfigParser
config = ConfigParser.RawConfigParser()
homepath = os.path.expanduser("~/")
configpath = homepath + ".config/scriptconfig/config.cfg"
config.read(configpath)
get = config.get('Config', 'Example')
print get
It should print the value of example
from the config file but when ran as sudo, the path is /home/root
so it doesn't find the config file.