1

The real account name of my windows is "Administrator.SC-201712271504", but os.getlogin() only gets "Administrator" and I can't export file to my desktop. The error is like No such file or directory: 'C:/Users/Administrator/Desktop/..."

How can I get the full account name "Administrator.SC-201712271504"?

Vito G.
  • 79
  • 1
  • 1
  • 8
  • how about using `getpass` module using `import getpass` and then `getpass.getuser()` – aayush_malik Nov 20 '19 at 06:16
  • Your user name is most likely "Administrator", and probably a new profile directory was created while upgrading an existing installation. The profile directory should be accurately reflected in the `USERPROFILE` environment variable, i.e. `os.environ['USERPROFILE']`. – Eryk Sun Nov 20 '19 at 10:02
  • That said, it's wrong to assume the default location of the "Desktop" directory. This is a relocatable user directory. The correct procedure is to ask the shell via [`SHGetKnownFolderPath`](https://learn.microsoft.com/en-us/windows/win32/api/shlobj_core/nf-shlobj_core-shgetknownfolderpath) or at a lower level via COM with the [`IKnownFolderManager`](https://learn.microsoft.com/en-us/windows/win32/api/shobjidl_core/nn-shobjidl_core-iknownfoldermanager) interface. See [here](https://stackoverflow.com/a/46391031/205580). – Eryk Sun Nov 20 '19 at 10:03

1 Answers1

1
import getpass
print(getpass.getuser())

or

import pwd
pwd.getpwuid(os.getuid())[0]
aayush_malik
  • 161
  • 13
  • what's the difference between os.getlogin() and getpass.getuser()? – Vito G. Nov 20 '19 at 06:25
  • 1
    `os.getlogin()` returns the name of the user logged in on the controlling terminal of the process, whereas `getpass.getuser()` checks `USERNAME` variable to find out who the user is. https://docs.python.org/3/library/os.html ... I used the latter because you said the former wasn't forming. Did `getpass.getuser` work? – aayush_malik Nov 20 '19 at 06:33
  • Can't test it right now since i have to convert my program to .exe and run it on my colleague's computer. Thank you. – Vito G. Nov 20 '19 at 06:47
  • 1
    @AMal, `os.getlogin` has nothing to do with a 'controlling terminal' in Windows. It calls [`GetUserNameW`](https://learn.microsoft.com/en-us/windows/win32/api/winbase/nf-winbase-getusernamew), which gets the user name from the logon session that's associated with the token of the current thread (if impersonating) or process. This logon session may or may not be the same as the logon session of the desktop session (i.e. Terminal Services session; the closest analog to a Unix 'controlling terminal'). – Eryk Sun Nov 20 '19 at 09:53
  • @Amal, the `USERNAME` environment variable is usually the same as the name returned by `GetUserNameW`, depending on context. But before it checks `USERNAME`, `getpass.getuser` checks for `LOGNAME`, `USER`, and `LNAME`, which is fundamentally wrong. These variables have no meaning in Windows, and are thus free to be used by programs or user sessions in ways that have nothing at all to do with `USERNAME`. – Eryk Sun Nov 20 '19 at 09:55