Literally, the file operation is nothing more than finding the file, opening the file, writing/deleting the file, finding the file and going to create the file, and nothing special. If that's what you think, it's naïve to provide a more logical way to manage data in Python. It's not as simple as you think.
The first is to open the file , open the file needs to call the Open function, provide the file name, and open the way. Open With (default to R):
r: read-only mode w: write-only mode A: Append mode (append from end of file) R+: Read/write mode w+: Read/write mode (will be emptied first, then written, then read, read more reasonable) a +: read/write mode (or append only from the end) RB: Binary read-only mode WB: Binary write-only mode ab: binary Append mode RB+: binary read/write mode WB +: binary read/write mode ab+: binary read/write mode
Read file
After you open a file in read mode, you can read the contents of the file.
Take the test.txt text as an example and read as follows:
1 Fukuda 2 Jingyuz 3 Music 4 doit
Test.txt
Here are a few common ways to read files
The first is to read all the contents of the file read ()
>>> f = open (' Test.txt ')
>>> a = F.read ()
>>> Print (a)
1 Fukuda
2 Jingyuz
3 Music
4 doit
>>> type (a)
<class ' str ' >
>>> A
' 1 fukuda\n2 jingyuz\n3 music\n4 doit '
>>> F.close ()
This way of reading, all the data in the file will be loaded into memory, the file is small good to say, the file is very sad, there may be a crash said, don't ask me why I know. And this function assigns all the text content to a string variable, yes, imagine a scene, a string with a capacity of dozens of G.
The first reading function to learn when I first approached Python was ReadLines (), which would also read all the contents of the file, but would be divided into lists by rows.
>>> f = open ('Test.txt','R')>>> A =F.readlines ()>>>type (a)<class 'List'>>>>print (a) ['1 fukuda\n','2 jingyuz\n','3 music\n','4 doit']>>> F.close ()
But in this way, too, there is the problem of reading large files out of memory, and Python provides the ReadLine () function, which you can read on a row-by-line basis.
>>> f = open ( " test.txt Span style= "color: #800000;" ", " r ' >>> a = F.readline () >>> while A: ... print (a,end =< Span style= "color: #800000;" > "" = F.readline () ... 1 Fukuda 2 Jingyuz 3 music 4 doit
>>>f.close ()
Yes, the feeling is particularly complex, and the efficiency is not particularly high. According to the official introduction, since the python2.5 version, the text stream has an iterative approach, that is, you can use a for loop to traverse the text stream, without worrying about memory or anything.
>>> f = open ('test.txt','r') for inch F: ... Print (Line,end="") ... 1 Fukuda 2 Jingyuz 3 Music 4 doit
Sometimes through the above reading method may appear garbled or something, this is due to different file encoding method. The file can be opened in binary mode (open (' test.txt ', ' RB ')) or directly in the open file is the specified enconding for which encoding method (open (' Test.txt ', ' R ', encoding= ' Utf-8 ')).
Write a file
You can write the file through the write mode operation, there are several points to note, W will first empty the original text file, and then write on the above, a will only be appended to the original file, r+ will be at the specified location to overwrite the way the content is written. Write operations are typically written to the file stream in a way called Date.write (str). The following is an example of a r+ operation Test.txt file
First row, second row, line fourth, row three.
Test.txt
>>> f = open ('test.txt','r+')>>> f.readline ()>>> b = F.tell ()>>> F.seek (b)8>>> F.write (" line fifth \ n")4>>> f.close ()
At this point, the Test.txt file becomes:
Row fifth, line fourth, line three
Test.txt
Here is a brief introduction to the file of the write operation, the above used two strange function tell and seek, these two functions can not use the mouse to locate the location of the Python code you want to reach, is very important to manipulate the text file function.
The first tell is used to get the position of the current read operation, the return value is the length of the current position (bytecode) or something, seek is to locate the location you want to go, and then go there, read and write operations, the combination of the two.
In addition, Python provides some additional functionality for file manipulation:
F.flush () writes the contents of the cache to the hard disk
F.next () returns to the next line and shifts the file action marker to the next line. When a file is used for a statement such as for ... in file, it is called the next () function to implement the traversal.
F.seekable () to see if the node can be positioned.
F.writable () to see if the file is writable
F.truncate () truncates the data after the pointer position, used in conjunction with seek
Pythoning--9:python file Operations