A system independent way using python to get the root directory/drive on which python is installed

前端 未结 4 558
遥遥无期
遥遥无期 2020-12-30 18:49

For Linux this would give me /, for Windows on the C drive that would give me C:\\\\. Note that python is not necessarily installed on the C drive

4条回答
  •  盖世英雄少女心
    2020-12-30 19:11

    Using pathlib (Python 3.4+):

    import sys
    from pathlib import Path
    
    path = Path(sys.executable)
    root_or_drive = path.root or path.drive
    

提交回复
热议问题