Python中的OS模块提供了与操作系统进行交互的函数。操作系统属于Python的标准实用程序模块。该模块提供了使用依赖于操作系统的函数的便携式方法。 os.path模块是Python中OS模块的子模块,用于通用路径名操作。
os.path.expanduser()
Python中的方法用于在用户主目录的给定路径中扩展初始路径组件〜(波浪号)或〜user。
在Unix平台上,如果已设置,则将初始〜替换为HOME环境变量的值。除此以外,os.path.expanduser()
方法搜索用户使用内置模块在密码目录中的主目录密码。包含首字母的路径〜用户直接在密码目录中查找该组件。
在Windows平台上,如果已设置,则将初始〜替换为HOME和USERPROFILE环境变量的值。否则,将使用HOMEPATH和HOMEDRIVE环境变量。而包含一个初始〜user组件的Path是通过从以上派生的路径中用〜user替换最后一个目录组件来处理的。
用法: os.path.expanduser(path)
参数:
path:代表文件系统路径的path-like对象。 path-like对象是表示路径的字符串或字节对象。
返回类型:此方法返回一个字符串值,该字符串值表示在给定路径中扩展初始路径分量〜或〜user后的路径。
代码1:使用os.path.expanduser()方法(在Unix上)
# Python program to explain os.path.expanduser() method
# importing os.path module
import os.path
# Path
path = "~/file.txt"
# Expand an intial ~ component
# in the given path
# using os.path.expanduser() method
full_path = os.path.expanduser(path)
# print the path after
# expanding the intial ~ component
# in the given path
print(full_path)
# Change the value of
# HOME environment variable
os.environ["HOME"] = "/home / GeeksForGeeks"
# Now, Expand the intial ~ component
# in the same path
# using os.path.expanduser() method
full_path = os.path.expanduser(path)
# print the path after
# expanding intial ~ 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 intial ~user
# component in the given path
# using os.path.expanduser() method
full_path = os.path.expanduser(path)
# print the path after
# expanding the intial ~user
# component in the given path
print(full_path)
/home/ihritik/file.txt /home/GeeksForGeeks/file.txt /home/ihritik/file.txt
代码2:使用os.path.expanduser()方法(在Windows上)
# 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:如果环境变量不存在
# 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
相关用法
- Python os.dup()用法及代码示例
- Python next()用法及代码示例
- Python set()用法及代码示例
- Python object()用法及代码示例
- Python bytes()用法及代码示例
- Python os.times()用法及代码示例
- Python os.chmod用法及代码示例
- Python hash()用法及代码示例
- Python os.ftruncate()用法及代码示例
- Python os.truncate()用法及代码示例
- Python os.fsdecode()用法及代码示例
- Python dict pop()用法及代码示例
- Python os.abort()用法及代码示例
- Python os.WEXITSTATUS()用法及代码示例
注:本文由纯净天空筛选整理自ihritik大神的英文原创作品 Python | os.path.expanduser() method。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。