一、
print(os.path.realpath(__file__)) # 当前文件的路径
print(os.path.dirname(os.path.realpath(__file__))) # 从当前文件路径中获取目录
print(os.path.basename(os.path.realpath(__file__))) # 从当前文件路径中获取文件名
输出结果如下:
D:informationjiekou-python3-masterjiekou-python3-masterexeeeee4.py
D:informationjiekou-python3-masterjiekou-python3-masterexeeee
e4.py
#当前文件的路径
pwd = os.getcwd()
#当前文件的父路径
father_path=os.path.abspath(os.path.dirname(pwd)+os.path.sep+".")
#当前文件的前两级目录
grader_father=os.path.abspath(os.path.dirname(pwd)+os.path.sep+"..")
输出
pwd: D:informationjiekou-python3-masterjiekou-python3-masterexeeee
father_path: D:informationjiekou-python3-masterjiekou-python3-master
grader_father: D:informationjiekou-python3-master
grader_father11=os.path.abspath(os.path.dirname(os.path.realpath(__file__))+os.path.sep+"..")
输出
grader_father11: D:informationjiekou-python3-masterjiekou-python3-master