This documents an unmaintained version of NetworkX. Please upgrade to a maintained version and see the current NetworkX documentation.


write_graph6(G, path, nodes=None, header=True)[source]

Write a simple undirected graph to a path in graph6 format.

  • G (Graph (undirected))
  • path (str) – The path naming the file to which to write the graph.
  • nodes (list or iterable) – Nodes are labeled 0…n-1 in the order provided. If None the ordering given by G.nodes() is used.
  • header (bool) – If True add ‘>>graph6<<’ string to head of data
  • NetworkXNotImplemented – If the graph is directed or is a multigraph.
  • ValueError – If the graph has at least 2 ** 36 nodes; the graph6 format is only defined for graphs of order less than 2 ** 36.


You can write a graph6 file by giving the path to a file:

>>> import tempfile
>>> with tempfile.NamedTemporaryFile() as f:
...     nx.write_graph6(nx.path_graph(2), f.name)
...     _ = f.seek(0)
...     print(f.read())  


The function writes a newline character after writing the encoding of the graph.

The format does not support edge or node labels, parallel edges or self loops. If self loops are present they are silently ignored.


[1]Graph6 specification <http://users.cecs.anu.edu.au/~bdm/data/formats.html>