Read operation of the file
Example:
1 Print("fetch of file handle, read operation:")2 3f = open ('Untitled','R', encoding='UTF8')4D =F.read ()5 f.close ()6 Print(d)7 8 Print('Case Two:')9f = open ('Untitled','R', encoding='UTF8')TenE = F.read (9) One f.close () A Print(e) - #Python3, one of the documents in both English and Chinese occupies a position of 1
Operation Result:
file handle fetch, read operation: Last night stars wind painting building West Bank Cassia East body no color Phoenix Double Fly Wing touches your heart- case two: Last night stars wind painting
Write Operations for files
Knowledge Points:
1. Before the write operation, the file is formatted to empty the file
2. Clear the operation, after executing the open W method, clear the
1 Print("Write operation, write file, cannot call read method, read the file, cannot call write method")2 3f = open ('python','W', encoding='UTF8')4F.write ("I must learn python \nbecause, Python is important \ n")5F.write ("Java is better?")6F.write ("maybe")#The above statement does not have a newline character, so the content of the output is immediately7F.close ()
Operation Result:
After opening the file, it appears as follows
is is Better?maybe
Append Methods for Files
Syntax format:
f = open (' filename ', 'a','encoding = UTF8 ')
File This method for Append mode: 1, the blank file, directly from the beginning to write the content, 2 of the content of the file, will begin to write at the end of the content
Example:
f = open ('python','a', encoding='UTF8 ') f.write (" blossom and flower ") f.close ()
Operation Result:
is is better?maybe flowers bloom and Flowers fall
ReadLine and ReadLines
ReadLine is read-line-wise
ReadLines is a full-text read
Example:
1 Print("ReadLine Method")2f = open ('Untitled','R', encoding='UTF8')3A =F.readline ()4 Print("at this point the cursor position:", F.tell ())5b =F.readline ()6 Print("at this point the cursor position:", F.tell ())7 Print(A.strip ())#Strip is a method of removing spaces and wrapping in string methods8 Print(B.strip ())9 Ten One Print("The ReadLines method will print the contents of each row as a list") Af = open ('Untitled','R', encoding='UTF8') -c =F.readlines () - Print(c) the Print(ID (c)) - Print(ID (f)) - forIinchC: - Print(I.strip ()) + Print("Traversal Method") - f.seek (0) + forIinchF: A Print(I.strip ()) atF.close ()#file operation, the close () method must not forget
Operation Result:
last night star last night wind painting building West Bank Cassia East ReadLines method, the contents of each row will be a list of printing [' last night the Stars wind \ n'' painting building West Bank Cassia East \ n' body without color Phoenix double wings \ n' touches your heart 378268245344280 last night the Stars last night wind painting building West Bank Cassia East body no color Phoenix Double flying wing touches your heart traverse method last night, the Stars last night wind painting building west Side Cassia East body no color Phoenix double wings touches your heart
The tell () and Seek () methods of the file
Example:
f = open ('Untitled','R', encoding='UTF8') F.read (4)Print('Current cursor Position', F.tell ()) F.seek (10)Print('Current cursor Position', F.tell ()) F.close ()#Read, a Chinese figure of three characters
Operation Result:
Current cursor position10
Flush method of file operation
Import Sys,time for in range: sys.stdout.write ("#") Sys.stdout.flush ( ) Time.sleep (1)
Truncate method
f = open ('Test','W') F.write ("Hello") F.write ("\ n") F.write ("python") F.flush ()#This allows the data in memory to be written to disk without executing the Close method.F.close () F= Open ('Test','a') F.truncate (2)#truncation method, the cursor is truncated from 2 onwardsF.close ()
Other file methods: r+ Read and Write methods
Re-learn Python-day 05-python basics, Python file operations: R, W, A, r+, A +, ReadLine, readlines, flush and other common file methods