當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


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。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。