IOError:[Errno 2]没有这样的文件或目录:'A.json'

时间:2018-08-24 23:18:32

标签: python json networkx mininet

能否让我知道如何解决此错误,我尝试运行python代码,但是当我运行代码时,它给出了about错误。我没有太多的编码经验,我确实需要解决此问题。 请提供帮助并为我提供有关修复方法的建议。

import networkx as nx

def path_valid(g, p):
""" Checks whether the list nodes p is a valid path in g."""
plen = len(p)
for i in range(plen-1):
 if not g.has_edge(p[i],p[i+1]):
     return False
return True

if __name__ == '__main__':
g = nx.Graph()  # Create an empty undirected graph
g.add_node(1); g.add_node(2); g.add_node(3)
g.add_edge(1,2); g.add_edge(1,3); g.add_edge(2,3)
print "Graph nodes are: {}".format(g.nodes())
print "Graph edges are: {}".format(g.edges())
print "Is edge (2,1) in the graph? {}".format(g.has_edge(2,1))
print "Is edge (3,2) in the graph? {}".format(g.has_edge(3,2))
print "Is path [1,3,2] valid? {}".format(path_valid(g, [1,3,2]))
print "Is path [1,4,2] valid? {}".format(path_valid(g, [1,4,2]))

from networkx.readwrite import json_graph
import json
g = nx.Graph()
# Don't need to add nodes separately.
g.add_edge(1,2, capacity=10)  # add a "capacity" parameter
g.add_edge(1,3, capacity=10)  # can have any name you like
g.add_edge(2,3, capacity=15)
print "Graph nodes are: {}".format(g.nodes())
print "Graph edges are: {}".format(g.edges(data=True))
print "Is edge (2,1) in the graph? {}".format(g.has_edge(2,1))
print "Is edge (3,2) in the graph? {}".format(g.has_edge(3,2))
print "The capacity of edge (2,3) is {}".format(g[2][3]["capacity"])
# print graph as a JSON string
# print graph as a JSON string
print json.dumps(json_graph.node_link_data(g),indent=4)
from networkx.readwrite import json_graph
import networkx as nx
import json
# Read in a JSON formatted graph file
g = json_graph.node_link_graph(json.load(open("A.json")))
print "graph nodes: {}".format(g.nodes())  # Lists nodes
print "graph links: {}".format(g.edges(data=True))  #  Shows link info
for n in g.nodes():  # See that hosts are assigned addresses
if g.node[n]["type"] == 'host':
    print "Host {}, IP = {}, MAC = {}".format(n, g.node[n]['ip'], g.node[n]['mac'])

我收到此错误:

File "/home/ubuntu/Desktop/my_python.py", line 42, in <module>
g = json_graph.node_link_graph(json.load(open("A.json")))
IOError: [Errno 2] No such file or directory: 'A.json'

如果您需要整个代码,请告诉我,以便我们将其发送给您。 感谢您的帮助和支持。

0 个答案:

没有答案