Python| os.path.expanduser() 方法
Python中的OS 模块提供了与操作系统交互的功能。操作系统属于 Python 的标准实用程序模块。该模块提供了一种使用操作系统相关功能的可移植方式。 os.path模块是Python中OS 模块的子模块,用于常见的路径名操作。
Python中的os.path.expanduser()方法用于将给定路径中的初始路径组件~ (波浪号)或 ~user 扩展到用户的主目录。
在 Unix 平台上,如果设置了HOME环境变量的值,则初始~将被替换。否则, os.path.expanduser()方法使用内置模块pwd在密码目录中搜索用户的主目录。直接在密码目录中查找包含初始~user组件的路径。
在 Windows 平台上,初始的~被HOME和USERPROFILE环境变量的值替换(如果已设置)。否则,将使用HOMEPATH和HOMEDRIVE环境变量。而包含初始~user组件的路径是通过将最后一个目录组件替换为上面派生的路径中的~ user 来处理的。
Syntax: os.path.expanduser(path)
Parameter:
path: A path-like object representing a file system path. A path-like object is either a string or bytes object representing a path.
Return Type: This method returns a string value which represents the path after expanding an initial path component ~ or ~user in the given path.
代码 #1:使用 os.path.expanduser() 方法(在 Unix 上)
Python3
# Python program to explain os.path.expanduser() method
# importing os.path module
import os.path
# Path
path = "~/file.txt"
# Expand an initial ~ component
# in the given path
# using os.path.expanduser() method
full_path = os.path.expanduser(path)
# print the path after
# expanding the initial ~ component
# in the given path
print(full_path)
# Change the value of
# HOME environment variable
os.environ["HOME"] = "/home / GeeksForGeeks"
# Now, Expand the initial ~ component
# in the same path
# using os.path.expanduser() method
full_path = os.path.expanduser(path)
# print the path after
# expanding initial ~ component
# in the given path
print(full_path)
# While expansion, An initial
# ~user component is looked
# up directly in the password directory.
# Path having an initial
# ~user component
path = "~ihritik / file.txt"
# Expand the initial ~user
# component in the given path
# using os.path.expanduser() method
full_path = os.path.expanduser(path)
# print the path after
# expanding the initial ~user
# component in the given path
print(full_path)
Python3
# Python program to explain os.path.expandvars() method
# importing os.path module
import os.path
# On Windows % name % expansions
# are supported in addition to
# $name and ${name}
# Path 1
path1 = R"% HOMEPATH %\Directory\file.txt"
# Path 2
path2 = R"C:\Users\$USERNAME\Directory\file.txt"
# Path 3
path3 = R"${TEMP}\file.txt"
# Expand the environment variables
# with their corresponding
# value in the given paths
exp_var1 = os.path.expandvars(path1)
exp_var2 = os.path.expandvars(path2)
exp_var3 = os.path.expandvars(path3)
# Print the given paths with
# environment variables expanded
print(exp_var1)
print(exp_var2)
print(exp_var3)
# In the above example
# os.path.expandvars() method
# replaced the environment variables
# 'HOMEPATH', 'USERNAME' and 'TEMP'
# with their corresponding values
Python3
# Python program to explain os.path.expandvars() method
# importing os.path module
import os.path
# If environment variable
# is malformed or does not exists
# then the given path will be
# left unchanged
# Path
path = R"${MYHOME}/Directory / file.txt"
# Expand the environment variables
# with their corresponding
# value in the given paths
exp_var = os.path.expandvars(path)
# Print the given patha with
# environment variables expanded
print(exp_var)
# As 'MYHOME' environment variable
# does not exists so
# os.path.expandvars() method
# will return the given path
# unchanged
/home/ihritik/file.txt
/home/GeeksForGeeks/file.txt
/home/ihritik/file.txt
代码 #2:使用 os.path.expanduser() 方法(在 Windows 上)
Python3
# Python program to explain os.path.expandvars() method
# importing os.path module
import os.path
# On Windows % name % expansions
# are supported in addition to
# $name and ${name}
# Path 1
path1 = R"% HOMEPATH %\Directory\file.txt"
# Path 2
path2 = R"C:\Users\$USERNAME\Directory\file.txt"
# Path 3
path3 = R"${TEMP}\file.txt"
# Expand the environment variables
# with their corresponding
# value in the given paths
exp_var1 = os.path.expandvars(path1)
exp_var2 = os.path.expandvars(path2)
exp_var3 = os.path.expandvars(path3)
# Print the given paths with
# environment variables expanded
print(exp_var1)
print(exp_var2)
print(exp_var3)
# In the above example
# os.path.expandvars() method
# replaced the environment variables
# 'HOMEPATH', 'USERNAME' and 'TEMP'
# with their corresponding values
\\Users\\Hritik\\\Directory\\file.txt
C:\\Users\\Hritik\\\Directory\\file.txt
C:\\Users\\Hritik\\AppData\\Local\\Temp\\file.txt
代码#3: 3:如果环境变量不存在
Python3
# Python program to explain os.path.expandvars() method
# importing os.path module
import os.path
# If environment variable
# is malformed or does not exists
# then the given path will be
# left unchanged
# Path
path = R"${MYHOME}/Directory / file.txt"
# Expand the environment variables
# with their corresponding
# value in the given paths
exp_var = os.path.expandvars(path)
# Print the given patha with
# environment variables expanded
print(exp_var)
# As 'MYHOME' environment variable
# does not exists so
# os.path.expandvars() method
# will return the given path
# unchanged
${MYHOME}/Directory/file.txt
参考: https://docs。 Python.org/3/library/os.path.html