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
  • 127
  • 1
  • 2
  • 14

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
  • 355,277
  • 75
  • 744
  • 836
-1

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

import commands
user_dir = commands.getoutput("cd; pwd")
Thomas
  • 8,306
  • 8
  • 53
  • 92