ReadLines
Just like read without parameters, ReadLines can read the entire file in a row and return a list with the data of each row being an element
# coding=utf-8 f = open ( " Test.txt ", " R " ) content = F.readlines () print (type (content)) I =1for temp in content: print ( " %d:%s "% (i, temp)) I +=1 f.close ()
readline
reads one row of data at a time
# coding=utf-8 f = open ( " Test.txt ", " R " ) content = F.readline () print ( " 1:%s " %content) content = F.readline () Span style= "COLOR: #0000ff" >print ( " 2:%s "%content) f.close ()
Tell
Get the current read and write location
In the process of reading and writing files, if you want to know the current location, you can use tell () to get
Seek
If you need to operate from another location while reading and writing files, you can use Seek ()
Seek (offset, from) has 2 parameters
Offset: Offsets
From: direction
0: Indicates the beginning of the file
1: Indicates the current position
2: Indicates end of file
When Offset>0, represents the right offset. When offset<0, indicates left offset
#open a file that already existsf = open ("Test.txt","R") Str= F.read (30)Print "The data read is:", str#Find Current LocationPosition =F.tell ()Print "Current file location:", Position#Reset LocationF.seek (5, 0)#Find Current LocationPosition =F.tell ()Print "Current file location:", Positionf.close ()
Python file Operation two