如何使用 networkX 在 Spyder、Anaconda 上读取 GML 文件

问题描述

我是编码的初学者。你能帮我如何使用networkX在Python Anaconda上读取/打印gml文件吗?我安装了库,代码如下所示:

import networkx as nx

g = nx.read_gml("gebaude.gml")

print(nx.info(g))

在我运行之后,控制台看起来像这样:

runfile('C:/Users/aksupi/Desktop/Experiment1.py',wdir='C:/Users/aksupi/Desktop')
Traceback (most recent call last):

  File "C:\Users\aksupi\Desktop\Experiment1.py",line 10,in <module>
    g = nx.read_gml("gebaude.gml")

  File "<decorator-gen-812>",line 2,in read_gml

  File "C:\Users\aksupi\anaconda3\lib\site-packages\networkx\utils\decorators.py",line 213,in _open_file
    fobj = _dispatch_dict[ext](path,mode=mode)

FileNotFoundError: [Errno 2] No such file or directory: 'gebaude.gml'
[enter image description here][2]

有什么问题?我需要将 gml 文件放在我电脑的什么位置?

我很期待很快收到您的来信。

最好的问候,

皮纳尔

解决方法

您可以使用文件的绝对路径(完整路径),或者如果您想使用相对路径(在本例中为名称),您需要相应地设置当前工作目录。在这种情况下,工作目录是 C:/Users/aksupi/Desktop(您可以在 Spyder 在运行文件时执行的 wdir 调用的 runfile 中看到它),因此该文件需要位于 {{1 }}(所以它的绝对路径是 C:/Users/aksupi/Desktop/)。

以防万一,在 Spyder 中,当前工作目录显示在右上角:

enter image description here