Python os.path.expanduser() - 展开初始路径组件

Python os.path.expanduser()

Python os.path.expanduser() 方法在Python中用于展开初始路径组件 ~ (波浪线符号)或~用户在给定的路径 user 的主目录。

在Unix平台上,首字母 ~ 的值来代替 HOME 如果设置了环境变量。否则, os.path.expanduser() 搜索方法 user 的主目录在密码目录中使用内置模块 pwd. 包含初始值的路径 ~user 组件直接在密码目录中查找。

在Windows平台上的首字母 ~ 的值来代替 HOME and USERPROFILE 如果设置了环境变量。否则, HOMEPATH 和 HOMEDRIVE 将使用环境变量。而Path包含一个初始值 ~user 组件通过将最后一个目录组件替换为 ~user 从上面导出的路径。

语法: os.path.expanduser(path)

参数:

path:表示文件系统路径的类路径对象。类路径对象可以是 string or bytes 对象,表示路径。

返回类型: 此方法返回一个字符串值,该值表示展开初始路径组件后的路径 ~ 或~用户在给定的路径。

示例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 initial ~ component
# in the given path
# using os.path.expanduser() method
full_path = os.path.expanduser(path)
  
# print the path after
# expanding the initial ~ component
# in the given path
print(full_path)
  
  
# Change the value of
# HOME environment variable
os.environ["HOME"] = "/home / GeeksForGeeks"
  
  
# Now, Expand the initial ~ component
# in the same path
# using os.path.expanduser() method
full_path = os.path.expanduser(path)
  
# print the path after
# expanding initial ~ 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 initial ~user
# component in the given path
# using os.path.expanduser() method
full_path = os.path.expanduser(path)
  
# print the path after
# expanding the initial ~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

如果环境变量不存在

# 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

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程