os.path.abspath获取上上级
时间: 2023-08-17 13:08:01 浏览: 109
可以使用`os.path.abspath`结合`os.path.dirname`来获取上上级目录的绝对路径。以下是一个示例代码:
```python
import os
current_dir = os.path.abspath(os.path.dirname(__file__))
parent_dir = os.path.abspath(os.path.join(current_dir, '..'))
grandparent_dir = os.path.abspath(os.path.join(parent_dir, '..'))
print(grandparent_dir)
```
在这个示例中,`os.path.abspath`用于获取当前脚本的绝对路径。然后,通过`os.path.dirname`获取当前目录的父目录路径。接着,使用`os.path.join`将父目录路径与当前目录路径拼接起来,得到上上级目录的路径。最后,使用`os.path.abspath`将路径转换为绝对路径。
请注意,在这个示例中,我使用了`__file__`来获取当前脚本的路径。如果你在交互式环境中运行代码,可能需要替换为你实际的脚本路径。
阅读全文
相关推荐










