write_gml(G, path, stringizer=None)[source]

Write a graph G in GML format to the file or file handle path.

  • G (NetworkX graph) – The graph to be converted to GML.
  • path (filename or filehandle) – The filename or filehandle to write. Files whose names end with .gz or .bz2 will be compressed.
  • stringizer (callable, optional) – A stringizer which converts non-int/non-float/non-dict values into strings. If it cannot convert a value into a string, it should raise a ValueError to indicate that. Default value: None.

NetworkXError – If stringizer cannot convert a value into a string, or the value to convert is not a string while stringizer is None.


Graph attributes named 'directed', 'multigraph', 'node' or 'edge',node attributes named 'id' or 'label', edge attributes named 'source' or 'target' (or 'key' if G is a multigraph) are ignored because these attribute names are used to encode the graph structure.

>>> G = nx.path_graph(4)
>>> nx.write_gml(G, "test.gml")

Filenames ending in .gz or .bz2 will be compressed.

>>> nx.write_gml(G, "test.gml.gz")