(The detailed operation of the read and write files) 2 python- file operation
1, the first six rows to read the file
f = open ("my-hert2","r") #encoding="utf-8" print(f.readline()) print(f.readline()) print(f.readline()) print(f.readline()) print(f.readline()) print(f.readline())
Another way:
f = open ("my-hert2","r") #encoding="utf-8" for i in range(6): print(f.readline())
Print results are as follows -----
Far East a dragon,
Its name is China
Far East there is a group of people,
They are all Chinese people
There are ancient oriental river
It's called the Yellow River
2, print all the contents of the file, but does not print ten lines
The first written
Open = f ( " My-Heart " , " r " ) for index, Line in the enumerate (f.readlines ()): # can read small files, large files will slow if the IF index == 9 : Print ( " - ----- parting line " ) Continue Print (line.strip ()) COUNT = 0
The second cycle of writing efficient
= COUNT 0 F = Open ( " My-Heart " , " R & lt " ) for Line in F: IF COUNT ==. 9 : Print ( ' ----- parting line ---- ' ) COUNT + =. 1 Continue Print (line.strip ()) # cancellation blank lines, and spaces count + = 1
Open the file position of the cursor
Open = F ( ' My-Heart ' , ' R & lt ' ) Print (f.tell ()) # returns the position of the cursor Print (reached, f.read (. 5)) # default all read, read-only part of the can, Print ( f.tell ()) ---------------- print result 0 Every . 5