How to change directory according to the username?

This question already has an answer here:

  • Is there a portable way to get the current username in Python? 11 answers

  • 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 .


    也许有更好的答案,但你总是可以使用命令调用:

    import commands
    user_dir = commands.getoutput("cd; pwd")
    
    链接地址: http://www.djcxy.com/p/79428.html

    上一篇: 如何让Python在Windows中获取用户名,然后在脚本中实现它

    下一篇: 如何根据用户名更改目录?