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

前端 未结 4 553
遥遥无期
遥遥无期 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:19

    Here's what you need:

    import sys, os
    
    def get_sys_exec_root_or_drive():
        path = sys.executable
        while os.path.split(path)[1]:
            path = os.path.split(path)[0]
        return path
    

提交回复
热议问题