Python os.path.expandvars() - 展开给定路径中的环境变量

Python os.path.expandvars()

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

如果给定的路径包含变形的环境变量名或不存在的环境变量,那么os.path.expandvars()方法将保持不变。

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

语法:os.path.expandvars(path)

参数:

path:表示文件系统路径的类路径对象。类路径对象是表示路径的字符串或字节对象。

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

示例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/Documents/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/Documents/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

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程