Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/309.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python IOError:[Errno 2]没有这样的文件或目录:';A.json';_Python_Json_Networkx_Mininet - Fatal编程技术网

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

Python IOError:[Errno 2]没有这样的文件或目录:';A.json';,python,json,networkx,mininet,Python,Json,Networkx,Mininet,您能告诉我如何修复这个错误吗?我正在尝试运行python代码,但是当我运行代码时,它给出了一个关于错误的提示。我没有太多的编码经验,我真的需要解决这个问题。 请帮助并建议我如何修理它 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_

您能告诉我如何修复这个错误吗?我正在尝试运行python代码,但是当我运行代码时,它给出了一个关于错误的提示。我没有太多的编码经验,我真的需要解决这个问题。 请帮助并建议我如何修理它

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'
文件“/home/ubuntu/Desktop/my_python.py”,第42行,在
g=json\u graph.node\u link\u graph(json.load(open(“A.json”))
IOError:[Errno 2]没有这样的文件或目录:“A.json”
如果您需要完整的代码,请告诉我,以便我可以将其发送给您。
感谢您的帮助和支持。

首先,您是否有名为
a.json
的文件?(请记住,Linux上的文件名区分大小写。)如果是,在运行Python脚本时,它是否与当前工作目录位于同一目录中?它只是缺少文件“A.json”——您有该文件吗?它试图在第42行打开并加载它,但由于找不到它而失败。我没有ut,请您现在检查代码,我只是添加了整个代码。非常感谢。