(continued from previous page seek( read( ' seek(- read( 'dgo to the th byte in the file go to the rd byte before the end in text files (those opened without in the mode string)only seeks relative to the beginning of the file are allowed (the exception being seeking to the very file end with seek( )and the only valid offset values are those returned from the tell()or zero any other offset value produces undefined behaviour file objects have some additional methodssuch as isatty(and truncate(which are less frequently usedconsult the library reference for complete guide to file objects saving structured data with json strings can easily be written to and read from file numbers take bit more effortsince the read(method only returns stringswhich will have to be passed to function like int()which takes string like ' and returns its numeric value when you want to save more complex data types like nested lists and dictionariesparsing and serializing by hand becomes complicated rather than having users constantly writing and debugging code to save complicated data types to filespython allows you to use the popular data interchange format called json (javascript object notationthe standard module called json can take python data hierarchiesand convert them to string representationsthis process is called serializing reconstructing the data from the string representation is called deserializing between serializing and deserializingthe string representing the object may have been stored in file or dataor sent over network connection to some distant machine notethe json format is commonly used by modern applications to allow for data exchange many programmers are already familiar with itwhich makes it good choice for interoperability if you have an object xyou can view its json string representation with simple line of codeimport json json dumps([ 'simple''list']'[ "simple""list"]another variant of the dumps(functioncalled dump()simply serializes the object to text file so if is text file object opened for writingwe can do thisjson dump(xfto decode the object againif is text file object which has been opened for readingx json load(fthis simple serialization technique can handle lists and dictionariesbut serializing arbitrary class instances in json requires bit of extra effort the reference for the json module contains an explanation of this see alsopickle the pickle module reading and writing files