Python tricks-detailed introduction of os.path library function usage

Python os.path library function usage detailed introduction

os.path.dirname(path)

  • Remove the file name and return to the directory
print(os.path.dirname("/Users/apple/test.py"))
#结果
/Users/apple

os.path.expanduser(path)

  • Expansion~
print(os.path.dirname("~/Documents"))
#结果
/Users/apple/Documents

os.path.abspath(path)

  • Return the absolute path of the file
print(os.path.dirname("四叶"))
#结果
/Users/apple/Documents/四叶

os.path.basename(path)

  • Return the file name at the end of the path (if the path ends with a'/', it returns empty)
print(os.path.dirname("~/Documents/test.py"))
#结果
test.py

Guess you like

Origin blog.csdn.net/fuhao7i/article/details/109577441