今天用到了获取当前路径 和 根目录,记录总结下:
1. 获取当前脚本的路径(包含文件名称)-绝对路径
a. os.path.abspath(__file__))
print(os.path.abspath(__file__))
b. os.path.realpath(__file__)
print(os.path.realpath(__file__))
结果:
2. 获取当前脚本所在目录(不包含当前文件名)
a. os.path.split(os.path.abspath(__file__))[0]
print(os.path.split(os.path.abspath(__file__))[0])
b. os.path.dirname(os.path.abspath(__file__))
print(os.path.dirname(os.path.abspath(__file__)))
结果:
3. 获取根目录,我的根目录是F:\Python\pythonexample\SafeUITesting
方法:
获取步骤2的结果,再使用一次 os.path.dirname()向上退一级;再利用 os.path.split()分割取第0个值即可
a. 得到当前目录: