[英]How to find the installation path of Windows OS using python?
I wanted to get the installation path of operation system on Windows. 我想获取Windows操作系统的安装路径。 For linux distribution, it would be /home/{username}/
. 对于linux发行版,它将是/home/{username}/
。 Is there a module to get this in python? 是否有模块可以在python中获得此功能? Or we need to manually write a script for it? 还是我们需要为此手动编写脚本? Example: I want : C:/.../Users/{username}/Desktop/
It's C: , I am interested in. 示例:我想要: C:/.../Users/{username}/Desktop/
是C C:/.../Users/{username}/Desktop/
我对此感兴趣。
I hope you mean user path, not the installation path. 我希望您的意思是用户路径,而不是安装路径。
In [33]: import os
In [35]: os.path.expanduser('~')
Out[35]: 'C:\\Users\\john'
1) Using os.environ
to read the WINDIR environment variable: 1)使用os.environ
读取WINDIR环境变量:
>>> os.path.split(os.environ['WINDIR'])[0]
'C:\\'
2) Using pathlib to get the current user directory (Python 3.5+): 2)使用pathlib获取当前用户目录(Python 3.5+):
>>> pathlib.Path.home().drive
'C:'
3) Or a mix of both, because the user directory could be on a different drive: 3)或两者兼而有之,因为用户目录可能位于其他驱动器上:
>>> pathlib.Path(os.environ['WINDIR']).drive
'C:'
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.