Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

python

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)

like image 823
KingMak Avatar asked Nov 30 '12 22:11

KingMak


People also ask

How do I find out my Windows password using Python?

The easiest way to acquire the password is to ask the user for it: input("Login Password: ") . And if, for some reason, you absolutely need to get the password without the users permission, it won't be as easy as your describing it. That's fine.

How do I find my current username?

Type whoami and press Enter. Your current user name will be displayed.

How do you make a Whoami in Python?

This function checks the environment variables LOGNAME, USER, LNAME and USERNAME, in order, and returns the value of the first one which is set to a non-empty string. If none are set, the login name from the password database is returned on systems which support the pwd module, otherwise, an exception is raised.


1 Answers

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.

like image 156
Gianluca Avatar answered Sep 22 '22 21:09

Gianluca