44

I know that using getpass.getuser() command, I can get the username, but how can I implement it in the following script automatically? So i want python to find the username and then implement it in the following script itself.

Script: os.path.join('..','Documents and Settings','USERNAME','Desktop'))

(Python Version 2.7 being used)

KingMak
  • 1,378
  • 2
  • 12
  • 26
  • 1
    Are you just asking how to put the result of `getpass.getuser()` in place of the string `'USERNAME'` in your command? If so, it's as simple as it sounds: just write `getpass.getuser()` where you've written `'USERNAME'`, and you're done. – abarnert Nov 30 '12 at 22:52
  • 1
    May be a duplicate of [this question](http://stackoverflow.com/questions/842059/is-there-a-portable-way-to-get-the-current-username-in-python) – cfi Mar 01 '14 at 18:22

10 Answers10

85

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.

Gianluca
  • 3,227
  • 2
  • 34
  • 35
  • 6
    The problem with this solution is, that you are language dependend. For example: The german windows name of "Documents and Settings" is "Dokumente und Einstellungen". The next point is, that you make another assumption about the Desktop directory. What, if you need access to the SendTo directory? On Windows XP and Windopws 7 they have totaly diffrent relativ pathes inside the userprofile. To overcome language und version dependencies, you should use call like those from win32com. Let windows give back the directoryname of the requested entity of the user (Desktop, SendTo, etc.) – f4m8 Sep 24 '13 at 13:31
  • 3
    I don't think this solution works in Windows, the python documentation says it is for Unix only. – Trishant Pahwa Apr 07 '19 at 07:49
  • 3
    @TrishantPahwa I don't see that in the documentation. I've tried both on Windows and they work fine. – Kemp Aug 09 '19 at 11:48
  • You can check it in the Python IDLE whether it gives you the output, when you use the function to return the username of the user. – Trishant Pahwa Aug 10 '19 at 07:07
  • yeah @TrishantPahwa, what are you talking about? it works well on Windows, even on windows 11 – Ice Bear Sep 01 '23 at 05:51
48

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

CodeJockey
  • 1,922
  • 1
  • 15
  • 20
9

for get current username: add import os in code and then use by :

print(os.getlogin())

OR

print(os.getenv('username'))

and if you get complete path from c drive use this :

print(os.environ['USERPROFILE']) #C:\Users\username
mamal
  • 1,791
  • 20
  • 14
7
>>> 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..

Joran Beasley
  • 110,522
  • 12
  • 160
  • 179
5

Install win32com, then:

from win32com.shell import shell, shellcon
print shell.SHGetFolderPath(0, shellcon.CSIDL_DESKTOP, None, 0)
Burhan Khalid
  • 169,990
  • 18
  • 245
  • 284
5

If you want the desktop directory, Windows 7 has an environment variable: DESKTOP:

>>> import os
>>> print(os.environ['desktop'])
C:\Users\KingMak\Desktop
Peter Wood
  • 23,859
  • 5
  • 60
  • 99
3

you can try the following as well:


import os
print (os.environ['USERPROFILE'])

The advantage of this is that you directly get an output like:

C:\\Users\\user_name
outcast_dreamer
  • 143
  • 2
  • 7
2

to get the current user directory you can also use this:

from os.path import expanduser
home = expanduser("~\buildconf")
cinatic
  • 861
  • 12
  • 19
2

import os followed by os.getlogin() works on macOS and Windows with python 3.7

After this you can do something like:

path = ''.join(('C:/Users/', os.getlogin(), '/Desktop/'))
Saurabh
  • 5,176
  • 4
  • 32
  • 46
1

This works for Python 3.* as well:

os.path.join(os.environ['HOME'] + "/Documents and Settings")
Daniel Trugman
  • 8,186
  • 20
  • 41
Natalia
  • 375
  • 3
  • 11