jupyter获取绝对路径
时间: 2024-01-29 18:10:44 浏览: 389
在Jupyter Notebook中获取当前文件所在目录的绝对路径,可以使用`os`模块的`path`方法中的`abspath()`函数。下面是一个示例代码:
```python
import os
print(os.path.abspath('.'))
```
这段代码会输出当前文件所在目录的绝对路径。请注意,`.`表示当前目录。
相关问题
jupyter notebook绝对路径
Jupyter Notebook是一个开源的交互式笔记本,可以在浏览器中创建和共享文档,其中包含实时代码、方程、可视化和说明文本。Jupyter Notebook的绝对路径是指文件在计算机文件系统中的完整路径,包括文件所在的文件夹和文件名。
在Jupyter Notebook中获取当前工作目录的绝对路径可以使用以下代码:
```python
import os
current_path = os.getcwd()
print(current_path)
```
这段代码会输出当前工作目录的绝对路径。如果你想获取其他文件或文件夹的绝对路径,可以使用`os.path.abspath()`函数来获取:
```python
import os
file_path = os.path.abspath("file_name")
print(file_path)
```
这段代码会输出指定文件名的绝对路径。请注意,你需要将"file_name"替换为你要获取绝对路径的文件名。
jupyter把相对路径转化为绝对路径,不使用os库
可以使用`os.path.abspath()`函数将相对路径转化为绝对路径,但是根据题目要求,不使用`os`库,可以使用以下方法:
```python
import os
def get_abs_path(rel_path):
abs_path = os.getcwd()
for path in rel_path.split('/'):
if path == '..':
abs_path = os.path.dirname(abs_path)
else:
abs_path = os.path.join(abs_path, path)
return abs_path
```
这个函数使用`os.getcwd()`获取当前工作目录作为绝对路径的起点,然后对于相对路径中的每一个路径段,如果是`..`则返回上一级目录,否则将其加入到绝对路径中。最后返回得到的绝对路径。
阅读全文
相关推荐













