1

I need to know how I can get the username that appears on the cmd with python, for example in this path:

C:\Users\john\Desktop\chrome.exe

I want to get the name John in this way, how can I do this with python?

C:\Users\Steve\Desktop\chrome.exe

How can I get the name with python?

martineau
  • 119,623
  • 25
  • 170
  • 301
Dkns
  • 45
  • 2
  • 10
  • 1
    Do you really want to analyze the command line, or do you want to know who actually ran the program? When `chrome.exe` is in `C:\Program Files` instead of a user's directory, analyzing the path won't help at all. – ShadowRanger Aug 26 '21 at 20:15
  • 2
    Does this answer your question? It is how to get the username of the currently logged in user with a Python package (not standard lib): https://stackoverflow.com/a/842096/9190640 – jorf.brunning Aug 26 '21 at 20:16
  • 1
    From the same thread, supposedly more secure and dependable: https://stackoverflow.com/a/65224170/9190640 – jorf.brunning Aug 26 '21 at 20:17
  • @ShadowRanger Like this? I'm trying to figure out the username to run the program that will be in the desktop area, if I have the username I'll be able to believe me – Dkns Aug 26 '21 at 20:25
  • Please provide enough code so others can better understand or reproduce the problem. – Community Aug 27 '21 at 03:49

3 Answers3

0

To get the username, you can use str.split:

path = "C:\\Users\\john\\Desktop\\chrome.exe"

user_name = path.split("Users\\")[1]
user_name = user_name.split("\\")[0]
print(user_name)
veedata
  • 1,048
  • 1
  • 9
  • 15
  • I think the question is how to get the currently logged in user, not how to clean a string. – jorf.brunning Aug 26 '21 at 20:15
  • Like I said... the username may be different, I don't know what it is for example, I want to be able to find out what the username appears on the cmd – Dkns Aug 26 '21 at 20:18
  • @Dkns I have updated my solution to get the username, let me know if that helps – veedata Aug 27 '21 at 07:04
0

First, you need to pass the command line value to a variable, and then you can do this:

s = 'C:\\Users\\john\\Desktop\\chrome.exe'
l = s.split('\\')
l[2].capitalize()

'John'

  • Like I said... the username may be different, I don't know what it is for example, I want to be able to find out what the username appears on the cmd – Dkns Aug 26 '21 at 20:18
  • On Linux systems you can read files /home/user/.bash_history for all users in your system, which stores all the commands entered by the user in the terminal. As soon as a new record appears there, then the user entered it in the terminal. Home directories are readable on many systems (for example, Ubuntu) – MikkiPython Aug 26 '21 at 21:23
0

You can automatize a little bit with the os library

import os

# get current directory
path = os.getcwd()

# split path into list
path_spit = path.split('\\')

# get index of Users word in the list
idx = path_split.index('Users')

# get user name
user = path_spit[ idx+1 ]
  • Can you enter the example output please? – Dkns Aug 27 '21 at 00:39
  • Because assuming I open the cmd in the directory C:\WINDOWS\system32 its code still working? I want to get the username regardless of the directory I am in – Dkns Aug 27 '21 at 00:52