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