reading and writing text files files are opened using the built-in open(functionwhich returns "file object(of type io textiowrapper for text filesthe open(function takes one mandatory argument--the filenamewhich may include path--and up to six optional argumentstwo of which we briefly cover here the second argument is the mode--this is used to specify whether the file is to be treated as text file or as binary fileand whether the file is to be opened for readingwritingappendingor combination of these character encodings for text filespython uses an encoding that is platform-dependent where possible it is best to specify the encoding using open()' encoding argumentso the syntaxes we normally use for opening files are thesefin open(filenameencoding="utf "fout open(filename" "encoding="utf "for reading text for writing text because open()' mode defaults to "read text"and by using keyword rather than positional argument for the encoding argumentwe can omit the other optional positional arguments when opening for reading and similarlywhen opening to write we need to give only the arguments we actually want to use (argument passing is covered in depth in once file is opened for reading in text modewe can read the whole file into single string using the file object' read(methodor into list of strings using the file object' readlines(method very common idiom for reading line by line is to treat the file object as an iteratorfor line in open(filenameencoding="utf ")process(linethis works because file object can be iterated overjust like sequencewith each successive item being string containing the next line from the file the lines we get back include the line termination character\ if we specify mode of " "the file is opened in "write textmode we write to file using the file object' write(methodwhich takes single string as its argument each line written should end with \ python automatically translates between \ and the underlying platform' line termination characters when reading and writing once we have finished using file object we can call its close(method--this will cause any outstanding writes to be flushed in small python programs it is very common not to bother calling close()since python does this automatically when the file object goes out of scope if problem occursit will be indicated by an exception being raised (file handling