StringIO
In many cases, the data read and write is not necessarily a file, you can also read and write in memory. StringIO is read in memory str
from IO Import StringIO F = StringIO () # Create Object StringIO I = f.write ( ' Hello ' ) # writes the string # Return Value: returns a string number I = f.write ( ' Li ' ) STR = f.getvalue () # read the string FF = the StringIO ( ' the Hello! \ Nhi! \ nGoodbye! ' ) S = ff.readline () # reads a line Print (S)
BytesIO
StringIO operation can only be str, if you are working with binary data, you need to use BytesIO
BytesIO achieve read and write bytes in memory