File (' Test.txt ', ' r ') read-only mode, is also the default way. (If the Test.txt file does not exist, it will error.) ) file (' Test.txt ', ' W ') write a file. (If you do not have this file, a file will be created in the current directory.) file (' Test.txt ', ' a ') Append method. File (' Test.txt ', ' W '). Write (' abcdefghkhjisorg ') put ABC ... Writes a test.txt file. The way it is written overwrites the old file. F=open ('/tmp/hello ', ' W ') Open (path + file name, read-write mode) with open (' Test.txt ', ' R ') as f: #这种方式写文件, do not close automatically save after writing. For i in f.readlines (): print i -------- ---------------------------------------------f=file (' Test.txt ', ' W ') #创建一个文件, the type is written to open f.write (' TODAY IS A&NBsp;good day\n ') #换行 f.close () # Close and save (to be closed in R). import tab #导入tab键f. Read () #读入写入的内容f. Flush () #刷新写入磁盘 (closed in R) F.tell () # Displays the first few characters now. F.seek (0) #移到指定的字符位置. For example, move to a 33rd character. F.seek () f.readline () #换行的方式读, read one line at a time. F.readlines () #以列表的方式读全部内容content =f.read () #把文件读的方式传给contentcontent # View content Contents Content.split (' \ n ') [0] #分行的方式查看文件, [0], the first line is viewed. [1] When viewing the second line. C=f.readlines () #cc [0]c[1] Modify the replacement file: for i in fileinput.input (' A.log ', inplace=1): i=i.replace (' Break ', ' Continue ') print i first back up and then modify the file: for i in fileinput.input (' A.log ', inplace=1,backup= '. Bak '): i=i.replace (' Break ', ' Continue ') print i
This article from "Small East elder brother" blog, declined reprint!
Section Sixth: Python file processing