How to get the home directory in Python?

This question already has an answer here:

  • How to find the real user home directory using python? 8 answers

  • You want to use os.path.expanduser. This will ensure it works on all platforms

    from os.path import expanduser
    home = expanduser("~")
    

    If you're on Python 3.5+ you can use pathlib.Path.home():

    from pathlib import Path
    home = str(Path.home())
    
    链接地址: http://www.djcxy.com/p/54556.html

    上一篇: 在Java中LBYL vs EAFP?

    下一篇: 如何在Python中获取主目录?