File Operation Trilogy: Open---operations--close.
A. File operation, several commonly used modes:
If you open the file, do not specify the mode of operation, the default is read;
For r/r+, if the file does not exist will be an error, the use of the need to ensure that the file exists;
For w/w+, if you use them, the contents of each run file will be overwritten;
For a/a+, this way appends the content, so the pointer ends.
* Note: When the file operation mode is a or a +, the file pointer at the end, if you want to read the contents of the file, remember to modify the position of the pointer, F.seek (0) This is the file pointer to the beginning of the place.
two. Several ways to manipulate files:
F.seek (), F.read (), F.readline (), F.readlines (), F.close (), F.write (), F.writelines (), F.truncate (), F.flush ()
1. To read the "test" file as an example, the file of various read operations:
1f = open ('Test','A +', encoding='Utf-8'# Open the file "test" to append content to the file2F.seek (0)#Place the file pointer at the beginning of the file3 Print('Read (): reads all the contents of the file')4 Print(F.read ())5 f.seek (0)6 Print('ReadLine (): reads only one line of content')7 Print(F.readline ())8 #f.seek (0) #不加这行的话f. ReadLines () outputs the remaining content9 Print('readlines (): Read all the contents of the file, put the contents of each line in the file into a list')Ten Print(F.readlines ()) OneF.close ()#Remember to close the file when you open it
This is the corresponding read result:
2. Loop Read file: Next ()
The File object in Python 3 does not support the next () method. The built-in function of Python 3 next () returns the next item by calling the __next__ () method from the iterator. In a loop, the next () method is called in each loop, which returns the next line of the file and, if it reaches the end (EOF), triggers the stopiteration
3. How to read files efficiently: For F in FW:
1FW = Open ('Test', encoding='Utf-8')2Count =13 #Loop the file object directly, that is, each line in the loop file4 forFinchFW:5 Print('Line No.%s'%count,f)6Count+=17Fw.close ()
4. Here are the actions to write to the file, flush the buffer, and empty the file:
Still take the test file above as an example,
1f = open ('Test','A +', encoding='Utf-8')#Open the file "test" to append content to the file2F.write ('Apple'+'\ n')#F.write () writes a string, \ n writes a newline3Tu= ('DDS','TTT','III')4F.writelines (TU)#when you write, pass in an iterative object5F.write ('\ n')6 Print(F.tell ())#returns the current location of the file. 7F.flush ()#refreshes the file internal buffer, directly writes the internal buffer data immediately to the file, rather than passively waits for the output buffer to write. 8 f.seek (0)9 Print(F.read ())TenF.truncate (10)#intercept a specified length One f.seek (0) A Print(F.read ()) - #f.truncate () #清空文件内容 -F.close ()#Remember to close the file when you open it
This is the corresponding result:
5. With operation files, automatically close files
When you manipulate a file, you often forget to close the file, so that you can use the with, which automatically closes the file after you use the file handle.
The specific use is as follows:
1With open ('Test','r+') as F:2res =F.read ()3New_res = Res.replace ('TTT',' Nice')#replace TTT with nice4 f.write (new_res)5 F.flush ()6 f.seek (0)7 Print(F.read ())
Use with for multi-file operations, as follows:
1With open ('Test','r+') as fr:2With open ('test_new','w+') as FW:#This is a multi-file operation, open two files, FR is read, FW is a new file written3 forLineinchFr#loop each line in the test file4New_line = Line.replace ('Hello','Good')5Fw.write (New_line)#write to the test_new file6
The above is a summary of the operation of the file.
Python file Operations