0

Is there are a way to change the user directory according to the username, something like

os.chdir('/home/arn/cake/')

But imagine that I don't know what's the username on that system. How do I find out what's the username, I know that python doesn't have variables so it's hard for me to get the username without variable.

SpringField
  • 119
  • 2
  • 13

2 Answers2

2
pwd.getpwnam(username).pw_dir

is the home directory of username. The user executing the program has username os.getlogin().

"I know that python doesn't have variables" -- that's nonsense. You obviously mean environment variables, which you can access using os.getenv or os.environ.

Fred Foo
  • 342,876
  • 71
  • 713
  • 819
-1

Maybe there is a better answer but you can always use command calls:

import commands
user_dir = commands.getoutput("cd; pwd")
Thomas
  • 7,686
  • 7
  • 47
  • 82