49

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)

2
  • 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. Commented Nov 30, 2012 at 22:52
  • 1
    May be a duplicate of this question Commented Mar 1, 2014 at 18:22

10 Answers 10

92

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.

Sign up to request clarification or add additional context in comments.

5 Comments

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.)
I don't think this solution works in Windows, the python documentation says it is for Unix only.
@TrishantPahwa I don't see that in the documentation. I've tried both on Windows and they work fine.
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.
yeah @TrishantPahwa, what are you talking about? it works well on Windows, even on windows 11
51

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

1 Comment

This is the only way to get it to work in IronPython that I have found.
11

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

Comments

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..

2 Comments

well this script worked too but it changes my directory which I don't want, thanks for posting though
what do you mean it changed your directory?
5

Install win32com, then:

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

Comments

5

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

>>> import os
>>> print(os.environ['desktop'])
C:\Users\KingMak\Desktop

Comments

4

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/'))

Comments

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

Comments

2

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

from os.path import expanduser
home = expanduser("~\buildconf")

Comments

1

This works for Python 3.* as well:

os.path.join(os.environ['HOME'] + "/Documents and Settings")

2 Comments

This does work, though what if there are multiple users, and what we are trying to find is the current user.
@TrishantPahwa the environment variables are set for the current user.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.