PDA

View Full Version : Noob python question



travmanx
December 1st, 2010, 06:00 AM
I am trying to get the current user on the machine which I know I can get from os.system() but when I execute the command

print 'Welcome %s' % (os.system('echo $USER'))

I get this in return

travis
Welcome 0
Script terminated.


-----

Why is the user being printed up top and a leading 0?

Thanks

TheWeakSleep
December 1st, 2010, 06:14 AM
When a command is completed succesfully, os.system() returns a 0

That means that the 'value' of os.system('echo $USER') is the 0, not the output of the command itself.

The 'travis' is just the output of the command.

try it for yourself! :)


os.system('echo $USER') == 0and it should print out 'True'

..unfortunately i don't know how to help other than that. If my explanation was wrong in anyway I'm sure someone will correct me.

EDIT: try os.getlogin() it should fix your problem :)

travmanx
December 1st, 2010, 06:22 AM
When a command is completed succesfully, os.system() returns a 0

That means that the 'value' of os.system('echo $USER') is the 0, not the output of the command itself.

The 'travis' is just the output of the command.

try it for yourself! :)


os.system('echo $USER') == 0

and it should print out 'True'

..unfortunately i don't know how to help other than that. If my explanation was wrong in anyway I'm sure someone will correct me.

Thank you :) I get it. It was a stupid mistake of mine in the beginning.

TheWeakSleep
December 1st, 2010, 06:24 AM
There are no stupid mistakes while you're learning :)

if you didnt see my edit, there is also from the getpass module

'import getpass'

you could use getpass.getuser() and that should be portable across *nix and windows. At least I think :p

DaithiF
December 1st, 2010, 09:34 AM
in python environment variables are stored in the os.environ dictionary.

so you can do:


import os
user = os.environ['USER']

TheWeakSleep
December 2nd, 2010, 12:25 AM
Wow how did I miss that! :o good to know!

Cheesehead
December 2nd, 2010, 08:19 PM
os.system(command)
Execute the command (a string) in a subshell. This is implemented by calling the Standard C function system(), and has the same limitations. Changes to sys.stdin, etc. are not reflected in the environment of the executed command.

On Unix, the return value is the exit status of the process encoded in the format specified for wait(). Note that POSIX does not specify the meaning of the return value of the C system() function, so the return value of the Python function is system-dependent. Source: http://docs.python.org/library/os.html



There are sveral ways to get the current username: http://stackoverflow.com/questions/842059/is-there-a-portable-way-to-get-the-current-username-in-python