os.path.expanduser(path)使用举例

参考链接: Python | os.path.expanduser() method
参考链接: os.path.expanduser到底有什么用?
参考链接: os.path.expanduser(path)

os.path.expanduser(path)使用举例_第1张图片

os.path.expanduser(path)使用举例_第2张图片

Python 3.7.4 (tags/v3.7.4:e09359112e, Jul  8 2019, 20:34:20) [MSC v.1916 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license()" for more information.
>>> import os
>>> os.path.expanduser('~')
'C:\\Users\\chenxuqi'
>>> 
>>> os.path.expanduser('~/')
'C:\\Users\\chenxuqi/'
>>> 
>>> os.path.expanduser('~data')
'C:\\Users\\data'
>>> 
>>> os.path.expanduser('~/data')
'C:\\Users\\chenxuqi/data'
>>> 
>>> os.path.expanduser(r'~\data')
'C:\\Users\\chenxuqi\\data'
>>> 
>>> os.path.expanduser('~\\data')
'C:\\Users\\chenxuqi\\data'
>>> 
>>> os.path.expanduser('data')
'data'
>>> os.path.expanduser('/data')
'/data'
>>> 
>>> 
>>> 
Python 3.7.4 (tags/v3.7.4:e09359112e, Jul  8 2019, 20:34:20) [MSC v.1916 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license()" for more information.
>>> import os
>>> os.path.expanduser('~')
'C:\\Users\\chenxuqi'
>>> os.path.expanduser('~/Project')
'C:\\Users\\chenxuqi/Project'
>>> 
>>> 
>>> path1 = R"%HOMEPATH%\Directory\file.txt"
>>> path2 = R"C:\Users\$USERNAME\Directory\file.txt"
>>> path3 = R"${TEMP}\file.txt"
>>> 
>>> path1
'%HOMEPATH%\\Directory\\file.txt'
>>> path2
'C:\\Users\\$USERNAME\\Directory\\file.txt'
>>> path3
'${TEMP}\\file.txt'
>>> 
>>> 
>>> exp_var1 = os.path.expandvars(path1)
>>> exp_var2 = os.path.expandvars(path2)
>>> exp_var3 = os.path.expandvars(path3)
>>> 
>>> 
>>> path1
'%HOMEPATH%\\Directory\\file.txt'
>>> exp_var1
'\\Users\\chenxuqi\\Directory\\file.txt'
>>> 
>>> 
>>> path2
'C:\\Users\\$USERNAME\\Directory\\file.txt'
>>> exp_var2
'C:\\Users\\chenxuqi\\Directory\\file.txt'
>>> 
>>> 
>>> path3
'${TEMP}\\file.txt'
>>> exp_var3
'C:\\Users\\chenxuqi\\AppData\\Local\\Temp\\file.txt'
>>> 
>>> 
>>> 

你可能感兴趣的:(os.path.expanduser(path)使用举例)