How to make Python get the username in windows and then implement it in a script

os.getlogin() did not exist for me. I had success with os.getenv('username') however.


os.getlogin() return the user that is executing the, so it can be:

path = os.path.join('..','Documents and Settings',os.getlogin(),'Desktop')

or, using getpass.getuser()

path = os.path.join('..','Documents and Settings',getpass.getuser(),'Desktop')

If I understand what you asked.


>>> os.path.join(os.path.expandvars("%userprofile%"),"Documents and Settings")
'C:\\Users\\USERNAME\\Documents and Settings'

should suffice ... I think thats what you actually meant anyway..


Install win32com, then:

from win32com.shell import shell, shellcon
print shell.SHGetFolderPath(0, shellcon.CSIDL_DESKTOP, None, 0)

Tags:

Python