当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


Python os.path.expandvars()用法及代码示例


Python中的OS模块提供了与操作系统进行交互的函数。操作系统属于Python的标准实用程序模块。该模块提供了使用依赖于操作系统的函数的便携式方法。 os.path该模块是Python中OS模块的子模块,用于通用路径名操作。

os.path.expandvars()Python中的方法用于扩展给定路径中的环境变量。它将给定路径中形式为$name或${name}的子字符串替换为环境变量name的值。

如果给定路径包含格式错误的环境变量名称或不存在的环境变量,则os.path.expandvars()方法将保持不变。


在Windows上,除了$name和${name}扩展之外,还支持%name%扩展。

用法: os.path.expandvars(path)

参数:
path:代表文件系统路径的path-like对象。 path-like对象是表示路径的字符串或字节对象。

返回类型:此方法返回一个字符串,该字符串表示环境变量已扩展的参数。

代码1:使用os.path.expandvars()方法

# Python program to explain os.path.expandvars() method  
    
# importing os.path module  
import os.path 
  
# Path 
path = "$HOME/file.txt"
  
# Expand the environment variables 
# with their corresponding  
# value in the given path   
exp_var = os.path.expandvars(path) 
  
  
# Print the given path with 
# environment variables expanded 
print(exp_var) 
  
  
# Change the value of  
# Environment variable 'HOME' 
  
os.environ["HOME"] = "/home/GeeksForGeeks" 
  
# Path 
path = "$HOME/Documnets/file.txt"
  
# Expand the environment variables 
# with their corresponding  
# value in the given path   
exp_var = os.path.expandvars(path) 
  
# Print the given path with 
# environment variables expanded 
print(exp_var) 
  
  
# Create an environment variable 
os.environ["USER"] = "ihritik"
  
# Path 
path = "/home/${USER}/file.txt"
  
# Expand the environment variables 
# with their corresponding  
# value in the given path   
exp_var = os.path.expandvars(path) 
  
# Print the given path with 
# environment variables expanded 
print(exp_var) 
  
  
# In the above example, 
# os.path.expandvars() method replaced  
# the environment variable 'HOME' and 'USER' 
# with their corresponding values
输出:
/home/ihritik/file.txt
/home/GeeksForGeeks/Documnets/file.txt
/home/ihritik/file.txt


代码2:os.path.expandvars()方法的使用(在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:如果环境变量不存在

# Python program to explain os.path.expandvars() method  
    
# importing os.path module  
import os.path 
  
# If the environment variable  
# is malformed or does not exist 
# 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 path 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



相关用法


注:本文由纯净天空筛选整理自ihritik大神的英文原创作品 Python | os.path.expandvars() method。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。