-1

I am trying to split a string taken from the cmd using os.system but no matter what I split by it always returns [0].

import os

username = os.system("whoami /user")
username = (str(username).split(' '))

print(username)

For some reason the original output is treated as an integer but that doesn't seem to matter as it is converted to a string.

string = "This is a string"
print(string.split(' '))

This works as intended though!

martineau
  • 119,623
  • 25
  • 170
  • 301

1 Answers1

2

os.system returns the exit code of the command, not the output. See this question for more detail (and code that does actually get the output). You don't need to run a command to get the username though, just use getpass.getuser():

import getpass
username = getpass.getuser()
Aplet123
  • 33,825
  • 1
  • 29
  • 55