File Streams

The file streams offered by SNAP.py are TFOut, which is used for file writing, and TFIn, which is used for file reading.

TFOut

class TFOut(FNm)

Creates a TFOut object that can be used to write the contents of the file specified by the path FNm. If a file with name FNm does not already exist, it creates a new file. If a file with name FNm exists, its contents will be overwritten.

Below is a list of functions supported by the TFOut class:

PutCh(Ch)

Writes the character Ch to the curret file stream position.

Flush()

Flushes the write buffer for the stream.

New(FNm)

Returns a new TFOut object for the file with name FNm.

Below is some code demonstrating the use of the TFOut type, which creates a file with name ‘test.graph’ in the working directory:

>>> Graph = snap.GenRndGnm(snap.PNGraph, 100, 1000)
>>> FOut = snap.TFOut("test.graph")
>>> Graph.Save(FOut)
>>> FOut.Flush()

TFIn

class TFIn(FNm)

Creates a TFIn object that can be used to read the contents of the file specified by the path FNm. Raises the exception RuntimeError if a file with name FNm cannot be found.

Below is a list of functions supported by the TFIn class:

Eof()

Returns a boolean indicating whether the end of the file has been reached.

Len()

Returns the length of the remainder of the file starting at the current file stream position.

GetCh()

Returns the next character in the file and updates the file stream position to the next character in the file.

PeekCh()

Returns the next character in the file without updating the file stream position.

Reset()

Resets the file stream to the beginning of the file.

New(FNm)

Returns a new TFIn object for the file with name FNm.

Below is some code demonstrating the use of the TFIn type, which assumes a file with name ‘test.graph’ exists in the working directory:

>>> FIn = snap.TFIn("test.graph")
>>> Graph = snap.TNGraph.Load(FIn)
>>> FIn.Len()
11445