This article mainly introduces the Python file operation instance code. if you need it, you can refer to this article to describe the Python file operation instance. 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)