import os
print('#########################')
# 当前路径 绝对路径 os.path.abspath(...)参数:path 相对路径转为绝对路径
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('#########################')
# 路径拼接 os.path.join(...) 参数:path1,path2,...
print("path is:\t", os.path.join('abc', 'def', 'hello.txt'))
print("path is:\t", os.path.join(os.path.abspath(".."), 'abc', 'def', 'hello.txt'))
print("path is:\t", os.path.abspath(os.path.join(os.getcwd(), './abc/def/')))
'''
运行结果:
#########################
path is: C:\python_code\example\path_example.py
path is: C:\python_code\example
path is: C:\python_code
path is: C:\
#########################
path is: C:\python_code\example
path is: C:\python_code\example\example\example.txt
#########################
path is: abc\def\hello.txt
path is: C:\python_code\abc\def\hello.txt
path is: C:\python_code\example\abc\def
'''
'''
参考:
https://blog.csdn.net/yannan20190313/article/details/130771876
https://zhuanlan.zhihu.com/p/614420191
https://www.runoob.com/python/python-os-path.html
'''