First, open the file: 文件句柄
=
open
(
‘文件路径‘
,
‘模式‘
)
There are two ways to open a file in Python, that is, open (...) and file (...), which in essence calls the latter in order to manipulate files, where we recommend using open to explain
Second, the operation of the document
The action file includes the read, write, and close of the file, first of all to talk about 文件句柄
=
open
(
‘文件路径‘
,
‘模式‘
Open mode: When we execute)操作的时候,要传递给open方法一个表示模式的参数:
The mode of opening the file is:
- R, read-only mode (default).
- W, write-only mode. "unreadable; not exist; create; delete content;"
- A, append mode. "Readable; not exist" create; "only append content;"
"+" means you can read and write a file at the same time
- r+, can read and write files. "readable; writable; can be added"
- w+, write and reread first. "This method opens the file to empty all the contents of the original file, writes the new content, and then reads what has been written."
- A +, with a
"U" means that when reading, \ r \ n \ r \ \ \ \ \ \ \ \ \ \ n (note: can only be used with R or r+ mode)
"B" means processing binary files (e.g. FTP send upload ISO image file, Linux can be ignored, Windows process binary file should be labeled)
The following is the source parsing of the file operation:
file Code
For the above source code in a method, you can look at the actual operation of the use case:
obj1 = open (' Filetest.txt ', ' w+ ') obj1.write (' I heard the Echo, from the valleys and the Heart\n ') obj1.writelines ([' open to The lonely soul of Sickle harvesting\n ', ' Repeat outrightly, but also Repeat the well-being of\n ', ' eventually swa Ying in the Desert Oasis ']) Obj1.seek (0) print obj1.readline () print Obj1.tell () print obj1.readlines () obj1.close ()
As an example of the open mode of ' w+ ', write is writing a string to the file, and Writelines is writing a string array to the file. The Seek (0) method is to point the pointer to the location, because in the process of writing, the pointer is marked with the content of the writing continues to move, the Seek method can move the pointer to the specified position, and this time point to the 0 position, starting from this position to read, you can read all the content just written. ReadLine () reads a row from the pointer position, so here the execution ReadLine reads the first line in the file that was just written, and the tell is the current position of the pointer, which is the time to execute the Say () method, where the pointer points to the start of the second row After the ReadLines method, the remainder of the file immediately after the current pointer is read into the array by line. Is the result of the file and console after the program executes:
Although just using the ' w+ ' way to open the file, but in fact this open way in file processing is not commonly used, was once rated as ' meaningless ' by our teacher, because the ' w+ ' method will empty the original file of everything ~
The above breath introduced so many methods, let us have a general concept, and then the various functions of these methods to take out the comparison:
Write file operations
Write,writelines, compared to those multifarious reading methods, writing method is more simple, only Wite and writelines two species. Look at the following example and the result of the write, in fact, the Write method and the Writelines method are similar, except that an accepted parameter is the list format, an accepted argument is a string format. Note the newline character when used here.
1 obj1 = open (' E:\PythonL\\11-8\\filetest.txt ', ' R ') 2 obj1 = open (' Filetest.txt ', ' w+ ') 3 obj1.write (' I heard the Echo, from The valleys and the Heart\nopen to the lonely soul of Sickle harvesting\n ') 4 obj1.writelines ([5 ' Repeat outrightly, b UT also repeat the well-being of\n ', 6 ' eventually swaying in the desert Oasis ' 7 ])
We have just used the write and Writelines method to write a small poem of Tagore to the file, the result is as follows:
I heard the echo, from the valleys and the Heartopen to the lonely soul of sickle harvestingrepeat outrightly, but also re Peat the well-being ofeventually swaying in the desert oasis
Read File operation
Let's take this document as an example to read the file:
First look at the method of reading all the contents of the file directly read and ReadLines, from the following results to know the two methods of a return list, one is the return string, and the above write-method corresponds to:
1 #readline方法2 obj1 = open (' E:\PythonL\\11-8\\filetest.txt ', ' R ') 3 print ' ReadLines: ', Obj1.readlines () 5 #readline方法6 Print "read:", Obj1.read ()
ReadLines ResultView Code
The ReadLines and read methods are simple and easy to use, but if the file is large, a one-time read-in memory slows down the performance of the program, and at this point we need a line of read files to reduce the memory usage.
Readline,next,xreadlines: Used to read a file by line, which requires a careful look at the use of Xreadlines, because Xreadlines returns an iterator and does not return the contents of a row directly
It's important to note that while I'm putting this big piece of code together, I'm going to get an error (valueerror:mixing iteration and read methods would lose data) if I really put this whole bunch of stuff together. The specific reasons are explained below.
1 obj1 = open (' E:\PythonL\\11-8\\filetest.txt ', ' R ') 2 #readline方法 3 print "ReadLine:", Obj1.readline () 5 #readline方法 6 Prin T "Next:", Obj1.next () 8 #readline方法 9 r = obj1.xreadlines () print ' Xreadlines: ', R.next () #readline方法13 print ' ReadLine S: ', Obj1.readlines () #readline方法16 print "read:", Obj1.read ()
Let's start by showing the results of executing the above programs:
The left is the code, and the right side is the corresponding execution result. Here we show the three methods of Readline,next,xreadlines.
Read result
One thing to add here is that the Xreadlines method was deprecated after python3.0, and it was replaced by a direct traversal of the FOR statement:
1 obj1 = open (' Filetest.txt ', ' R ') 2 for line in Obj1:3 print line 4 5 run Result: 6 I heard the echo, from the valleys And the Heart 7 8 Open to the lonely soul of sickle harvesting 9 Repeat outrightly, but also Repeat the well-being Of11 eventually swaying in the desert oasis
A pointer in a file
After reading the file read and write, the basic operation of the file we solved, the following describes the file processing and pointers related methods: Seek,tell,truncate
1 obj1 = open (' Filetest.txt ', ' w+ ') 2 obj1.write (' I heard the Echo, from the valleys and the heart\n ' 3 ' open to the lonely s Oul of Sickle harvesting\n ') 4 print ' 1.tell: ', Obj1.tell () 5 obj1.writelines ([6 ' Repeat outrightly, but also Repeat the Well-Being of\n ', 7 ' eventually swaying in the desert Oasis ' 8 ]) 9 print ' 2.tell: ', Obj1.tell ()
First of all, see Tell,tell's role is to indicate where the current pointer is located. Regardless of whether the file is read or written, it depends on the position of the pointer, we start reading from the position of the pointer, and start writing from the position of the pointer. Let's write the results of the tell in the middle of the previous content. After executing the code, the results are as follows:
1.tell:962.tell:188
Then look at the use of seek:
1 obj1 = open (' E:\PythonL\\11-8\\filetest.txt ', ' R ') 2 print "Next:", Obj1.next (), ' tell1: ', Obj1.tell (), ' \ n ' 3 Obj1.seek ( 4 print "read:", Obj1.read (), ' tell2: ', Obj1.tell (), ' \ n '
Seek use
From the execution results shown, we use the Tell method when we use next to read the file, this time the return is 188, the pointer has pointed to the end of the tell (for specific reasons explained below), then we execute the Read method, the content is not available, This time we use the Seek method to point the pointer to the 50 position, and then use the Read method, you can read the rest of the content.
Looking at an example of truncate:
1 obj1 = open (' Filetest.txt ', ' r+ ') 2 3 Obj1.write (' This is a truncate test,*** ') 4 obj1.seek (0) 5 print ' first read:\n ', Obj1.read () 6 7 obj1.seek (0) 8 Obj1.write (' This is a truncate test ') 9 obj1.truncate () obj1.seek (0) print ' \nsec Ond read:\n ', obj1.read ()
truncate result
With the above print results, we can know that in the file to write operations, the position of the pointer will be directly covered by the corresponding content, but many times after we modify the file, the following things do not want to retain, this time we use the Truncate method, the file will only save the current pointer position before the content. We can also use TRUNCATE (n) to hold the content before N, where n represents the pointer position.
With Operation files
To avoid forgetting to close after opening a file, you can manage the context by using the: with open (' File path ', ' Operation mode ') as file handle:
1 #使用whith打开可以不用close2 with open (' E:\PythonL\\filetest.txt ', ' R ') as File_obj:3 file_obj.write (") 4 5 #在Python 2.7 after , with and support for managing the context of multiple files simultaneously, the following example opens two files at a time
6 #with Open (' E:\PythonL\\filetest1.txt ', ' R ') as File_obj1,open (' E:\PythonL\\filetest2.txt ', ' W ') as File_obj2: '
Errors that are easy to make:
Valueerror:mixing iteration and Read methods would lose data
I have encountered such a problem in the process of manipulating files, which literally means that the pointer is wrong, so how does this problem arise? I found that this error occurs after using the next or Xreadlines method and then using the Read or ReadLines method, because next or xreadlines includes the way we normally use the For loop to read the file. The program maintains a pointer within itself (which also explains how the pointers we get with the Tell method when we use these methods are all pointing at the end of the file, not the current unique location), so if we want to read a line first using the next or Xreadlines method above, Then use the Read or ReadLines method to read the rest of the content will be an error.
Solution:
There are two types of solutions at this time:
First, after reading a line, using seek to specify the position of the pointer, you can continue to use other methods
The second, using the ReadLine method, this method does not have internal maintenance of the pointer, it is spicy simple line of silly reading, the pointer will be a silly line down to move. This time you can also use the Tell method to track the correct position of the pointer, you can also use the Seek method to locate the location, with Truncate,wirte and other methods, can not be better use some.
Python file operations