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
TFOutobject 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
TFOutclass:-
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
TFOutobject for the file with name FNm.
Below is some code demonstrating the use of the
TFOuttype, which creates a file with name ‘test.graph’ in the working directory:>>> Graph = snap.GenRndGnm(snap.TNGraph, 100, 1000) >>> FOut = snap.TFOut("test.graph") >>> Graph.Save(FOut) >>> FOut.Flush()
-
TFIn¶
-
class
TFIn(FNm)¶ Creates a
TFInobject that can be used to read the contents of the file specified by the path FNm. Raises the exceptionRuntimeErrorif a file with name FNm cannot be found.Below is a list of functions supported by the
TFInclass:-
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
TFInobject for the file with name FNm.
Below is some code demonstrating the use of the
TFIntype, 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
-