Detailed descriptions of Python file operation operations instances,
This article describes the Python file operation examples. The detailed code is as follows:
#! /Usr/bin/env python #! /Usr/bin/env python # coding: UTF-8 # Purpose: file Operation class # declare a string text poem = ''' Programming is fun test When the work is done if you wanna make your work also fun: use Python! ''' # Create a file class instance in the read-only mode ('R'), write mode ('W'), and append mode ('A ') f=file('poem.txt ', 'A') # open for 'W' riting f. write (poem) # write text to the file write text to file f. close () # close the file # the default is read-only mode f1_file('poem.txt ') # if no mode is specified, 'R' ead mode is assumed by default while True: line = f. readline () # Read every row of the file if len (line) = 0: # Zero length indicates EOF break print line, # output this line # note, because the content read from the file has ended with a line break, we are on the output statement. Use commas to remove line breaks. # Notice comma to avoid automatic newline added by Python f. close () # close the file # help (file)