0

I would like python to find the current user's username and then implement it into the following line of code.

I have used multiple suggestions given on here but they don't seem to work, the only thing that works is when I manually enter the username in below line of code starting with root = filedialog… then the folder opens up in the C:\Users\username\Documents as it should.

from tkinter import *
from tkinter import filedialog
import getpass
import os

gui = Tk()
gui.geometry('200x400')

#user input
root = filedialog.askdirectory(initialdir="C:\\Users\\username\\Documents", title="Select your root 
folder")

def printName(event):
    print("Config folder created")
    #Create a folder structure
    project = "1"
    app = "bl"
    mw = "jb"
    a = "modules"
    b = "com"
    c = "mac"
    d = "f"
    e = "config"
    f = "main"

    path = f"{root}/{project}/{app}/{mw}/{a}/{b}/{c}/{d}/{e}/{f}".lower().replace(" ","")
    print(path)

    #If path exists don't create if not create path
    if not os.path.exists(path):
        os.makedirs(path)
        print("Config folder created")

button_1 = Button(gui, text="Config folder")
button_1.bind("<Button-1>", printName)
button_1.pack()

gui.mainloop()
martineau
  • 119,623
  • 25
  • 170
  • 301
TJ44
  • 3
  • 3
  • On Windows there's an environment variable named `USERNAME` you could use — i.e. the value `os.environ['USERNAME']`. It's probably in the Windows registry somewhere, too, to that's another way. – martineau Jan 19 '21 at 23:58
  • Open a command prompt and issue `set USER` command and see whether `USERNAME` and `USERPROFILE` environment variables are set or not. – acw1668 Jan 20 '21 at 03:00
  • This is asking the wrong question. You are looking for the user-configurable location of a well-known folder. [Working with Known Folders in Applications](https://learn.microsoft.com/en-us/windows/win32/shell/working-with-known-folders) explains how to do that properly. – IInspectable Jan 20 '21 at 03:16
  • Yes if I go to CMD and type set user the the USERNAME and USERPROFILE environment variables are set. However when I use os.environ['USERNAME'] in the code above it still doesn't work. Looking to pass this programme on so whoever has this programme it will work on their PC and pick up their respective user name. – TJ44 Jan 20 '21 at 22:09
  • Note that the name of your home directory under `C:\Users` may not exactly the same as the value of env `USERNAME`. My username is "acw1668", but the name of the home directory inside `C:\Users` is "acw16". Better use env `USERPROFILE`. Also update your question how you use `os.environ[...]`. – acw1668 Jan 21 '21 at 03:09
  • Many thanks acw that worked! This now opens in the USERPROFILE C:\Users\username, I tried adding on \\Documents but that didn't work anything else I have to add on in order for it to open in the username's documents folder? – TJ44 Jan 21 '21 at 08:55
  • `initialdir=f"{os.environ['USERPROFILE']}\\Documents"` should work. – acw1668 Jan 22 '21 at 03:37
  • Yes ACW that works perfectly now thanks! Was there anything I was doing wrong? – TJ44 Jan 26 '21 at 20:45

0 Answers0