This article describes the operation of the Python file operation class, the detailed code is as follows:
#!/usr/bin/env python#!/usr/bin/env python #coding: Utf-8 # Purpose: File Operation class # declares a string literal poem= ' ' programming is a fun test when the Work is do if you wanna make your work also fun:use python! "#创建一个file类的实例, the mode can be: read-only mode (' R '), write mode (' W '), Append mode (' a ') f=file (' Poem.txt ', ' a ') #open for ' W ' riting F.write (poem) # Write text to file write F.close () #关闭文件 Close the file# default is read-only mode f=file (' Poem.txt ') # If no mode is specified, ' R ' ead mode is assumed by default while True:line=f.readline () #读取文件的每一个行 If Len ==0: # Zero length indicates EOF break print L INE, #输出该行 #注意 because the content read from the file already ends with a newline character, so we use commas on the output statement to eliminate the wrapping. #Notice comma to avoid automatic newline added by Python f.close () #close the file# Assist Help (file)