I have a python program tha save useful information to ini file in home directory. I would like the way to recognize the username of gnu/linux machine then put it in variable. It is very important that the program run sudo command so always I run like sudo python3 myapp.py.
settings.py
import os
import configparser
uid = 1000 # non-privileged user
gid = 1000 # non-privileged user
username = os.getlogin()
print(username)
# Create directory
dirName = '/home/$username/.config/macspoof'
^^^^^ <--- How to get output of var
# Create target directory & all intermediate directories if don't exists
try:
os.makedirs(dirName)
os.chown(dirName, uid, gid)
print("Directory ", dirName, " Created ")
except FileExistsError:
print("Directory ", dirName, " already exists")
filename = '/home/user/config/macspoof/macspoof.ini'
# Writing Data
config = configparser.ConfigParser()
config.read(filename)
....
....
....