This article describes an operation instance of the Python file action class with detailed code as follows:
#!/usr/bin/env python
#!/usr/bin/env python
#coding: Utf-8
# Purpose: File operation class
#声明一个字符串文本
poem= ' "
programming is fun test
When the work are done
if your wanna make your work also fun: Use
python!
'
' #创建一个file类的实例, the pattern can be read-only (' R '), write mode (' W '), Append mode (' a ')
f=file (' poem.txt ', ' a ') #open for ' W ' riting
f.write ( Poem) #写入文本到文件 write text to file
f.close () #关闭文件 Close the file
#默认是只读模式
f=file (' Poem.txt ')
# if no m Ode is specified, ' R ' ead "mode is assumed by default" while
True:
line=f.readline () #读取文件的每一个行
If len = =0: # Zero length indicates EOF
breaks print line, #输出该行
#注意 because the content read from the file has ended with a newline character, so we use commas on the output statement to eliminate the word wrap.
#Notice comma to avoid automatic newline added by Python
F.close () #close the file
#帮助 help
(file)