API文档:
On Unix and Windows, return the argument with an initial component of ~ or ~user replaced by that user‘s home directory.
On Unix, an initial ~ is replaced by the environment variable HOME if it is set; otherwise the current user’s home directory is looked up in the password directory through the built-in module pwd. An initial ~user is looked up directly in the password directory.
On Windows, HOME and USERPROFILE will be used if set, otherwise a combination of HOMEPATH and HOMEDRIVE will be used. An initial ~user is handled by stripping the last directory component from the created user path derived above.
If the expansion fails or if the path does not begin with a tilde, the path is returned unchanged.
文档翻译:
在Unix和Windows,~返回用户主目录
如果参数为~标示用户主目录,否则返回参数字符串的目录
例子:
import os
print os.path.expanduser('~')
print os.path.expanduser('c:\\')
输出:
C:\Users\lyle2000w
c:\