import os
print('#########################')
# 当前路径 绝对路径
print("path is:\t", os.path.abspath(__file__))
# 当前路径 绝对路径 上一级目录
print("path is:\t", os.path.abspath("."))
# 当前路径 绝对路径 上上一级目录
print("path is:\t", os.path.abspath(".."))
# 当前路径 绝对路径 上上一级目录
print("path is:\t", os.path.abspath("..\.."))
print('#########################')
# 当前目录
print("path is:\t", os.getcwd())
print("path is:\t", os.path.abspath("example/example.txt"))
print('#########################')
# 将目录和文件名合成一个路径
print("path is:\t", os.path.join('abc', 'def', 'hello.txt'))
'''
运行结果:
#########################
path is: C:\Python\PythonCode\Test\selenium_example.py
path is: C:\Python\PythonCode\Test
path is: C:\Python\PythonCode
path is: C:\Python
#########################
path is: C:\Python\PythonCode\Test
path is: C:\Python\PythonCode\Test\example\example.txt
#########################
path is: abc\def\hello.txt
'''
'''
参考:
https://zhuanlan.zhihu.com/p/614420191
https://www.runoob.com/python/python-os-path.html
'''