Original chain: http://www.cnblogs.com/vamei/archive/2012/06/06/2537868.html
The first part:
#file read/write#f = open (file name, mode)#"R" read-only#"W" Write writesF= Open ("Test.txt","R")#Open Test.txt file, read-only mode#ReadContent =F.read (N)#read n bytes of data#the read ([size]) method reads a size byte from the current location of the file#If no parameter size is read to the end of the file, it is returned as a string objectcontent=F.readline ()#read a row#returns a String object#more suitable for large filesConten=F.readlines ()#Read all Rows#return list, each element is a row#large files Take time#WriteF.write ('I like Apple')#writing a string to a file#Close FileF.close" "the job builds a record.txt document that reads as follows: Tom, 86Lee, 99Lucy, 19, 56, read the file from the Record.txt and print it ." "" "additional Information linecache module output file line 2nd Text = linecache.getline (' A.txt ', 2) print (text)" "
Part II: Operations
File = Open ('D:/record.txt','W')Print(File.name) file.write ("' tom,12,86 ', ' lee,15,99 ', ' lucy,11,58 ', ' joseph,19,56 '") file.close () file= Open ('D:/record.txt','R') Lines=File.readlines ()Print(lines)#the run job is annotated#wrong line, newline character \ n#ModifyFile = Open ('D:/record.txt','W')Print(File.name) file.write ('tom,12,86\nlee,15,99\nlucy,11,58\njoseph,19,56') file.close () file= Open ('D:/record.txt','R') Lines=File.readlines ()Print(lines)#accomplished
Online editor run diagram, no directory, otherwise will be error
Modify the previous D drive File open diagram
Modified D-Drive File open diagram
Python Learning Notes (12) file read/write