operation interpretation output open( ' :\spam'' 'create output file ('wmeans writeinput open('data'' 'create input file ('rmeans readinput open('data'same as prior line ('ris the defaultastring input read(read entire file into single string astring input read(nread up to next characters (or bytesinto string astring input readline(read next line (including \ newlineinto string alist input readlines(read entire file into list of line strings (with \noutput write(astringwrite string of characters (or bytesinto file output writelines(alistwrite all line strings in list into file output close(manual close (done for you when file is collectedoutput flush(flush output buffer to disk without closing anyfile seek(nchange file position to offset for next operation for line in open('data')use line file iterators read line by line open(' txt'encoding='latin- 'python unicode text files (str stringsopen(' bin''rb'python bytes files (bytes stringscodecs open(' txt'encoding='utf 'python unicode text files (unicode stringsopen(' bin''rb'python bytes files (str stringsopening files to open filea program calls the built-in open functionwith the external filename firstfollowed by processing mode the call returns file objectwhich in turn has methods for data transferafile open(filenamemodeafile method(the first argument to openthe external filenamemay include platform-specific and absolute or relative directory path prefix without directory paththe file is assumed to exist in the current working directory ( where the script runsas we'll see in ' expanded file coveragethe filename may also contain non-ascii unicode characters that python automatically translates to and from the underlying platform' encodingor be provided as pre-encoded byte string the second argument to openprocessing modeis typically the string 'rto open for text input (the default)'wto create and open for text outputor 'ato open for appending text to the end ( for adding to logfilesthe processing mode argument can specify additional optionsfiles