-2

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)
....
....
....
  • 2
    You're losing some info there, like what happens? What is the actual problem? Right now you have a bunch of requirements and some code. – Mad Physicist May 07 '22 at 15:11
  • @MadPhysicist Now I put home directory myself. I would like to recongize the name of "user" automatically. Example if the name of linux machine is "mad" the full path is `/home/mad/.config/macspoof.ini` Now I make full path with name mad myself. – user14795102 May 07 '22 at 15:21
  • You mentioned what you want, and dumped some code. But you don't explain what the code does, or for that matter why it's there. Please see [ask] – Mad Physicist May 07 '22 at 15:31
  • `dirName = '/home/$username/.config/macspoof'` – user14795102 May 07 '22 at 15:31
  • Why is this tagged "bash"? The only code in it is Python. – Charles Duffy May 07 '22 at 15:34
  • BTW, `sudo` sets environment variables describing who it was run by. Dump `os.environ` when your program was started by sudo, and take a look through it. – Charles Duffy May 07 '22 at 15:35
  • (if you're more willing to be Linux-only, you can also ask Python for your parent process's PID, and `stat` the entry in procfs for that PID to get ownership information; but usually that's overkill). – Charles Duffy May 07 '22 at 15:37
  • Hint: [How to get sudo username in python](https://stackoverflow.com/questions/43929714/how-to-get-sudo-username-in-python). The OP has a different goal than you do, but that just means that many of the attempted approaches they consider unsuccessful would be _successful_ approaches to you. – Charles Duffy May 07 '22 at 15:38
  • @CharlesDuffy I saw the the link . I did't make it in my app. I didn't understand how to put variable in full path. I want the username to put in full path ....`dirName = '/home/$username/.config/macspoof'` – user14795102 May 07 '22 at 15:45
  • @user14795102, `dirname = f'/home/{ os.environ.get("SUDO_USER", os.getlogin()) }/.config/macspoof'` -- so if there exists a `SUDO_USER` we use it as the username; if there isn't one we use `os.getlogin()` instead. – Charles Duffy May 07 '22 at 15:46
  • @user14795102, or, y'know, `username = os.environ.get('SUDO_USER', os.getlogin())`, and then later `dirname = os.path.join(os.path.expanduser('~'+username), '.config/macspoof')` – Charles Duffy May 07 '22 at 15:49
  • but really, at that point this isn't a question about sudo or even about usernames specifically, but about "how do I substitute a variable as part of a path?"; the site is full of examples teaching that already, and I've added one as an additional duplicate. – Charles Duffy May 07 '22 at 15:49
  • BTW, note that home directories aren't guaranteed to only ever be under `/home`. Maybe someday your code gets run by someone whose storage is on a NAS, so their home directory is `/mnt/storage/homes/USERNAME`; or on MacOS, where it's `/Users/USERNAME`. Always better to do a proper lookup than to assume. – Charles Duffy May 07 '22 at 15:51
  • @CharlesDuffy My english is not very well. You are very hard. I cant explain exactly what I want, because I am not native speaker! What of all of above I have to keep? I want to change filename too. – user14795102 May 07 '22 at 15:57

0 Answers0