python2.7
1. Opening of the file (built-in function)
Open (file_path,mode= ' R ', Buffering=-1)
<1> File_path is the parameter that must be given, which is the absolute or relative path to the file to be read, including the file suffix.
3 ways to represent absolute paths:>>> File_path = "C:/tmp/123.txt" >>> file_path = "C:\\tmp\\123.txt" >>> fil E_path =r "C:\tmp\123.txt"
Two representations of relative paths: ./ represents the current folder. / indicates parent folder
The file path is a string, so pay special attention to how the string contains a backslash when it should be correctly represented. ' \ \ ' really means ' \ ', the modifier ' R ' represents the original word character in Python.
<2>mode points out how the file is opened and how it is opened, no longer cumbersome.
<3> Buffering parameter indicates the buffering option when reading a file
| |
| buffering = 1 |
cache line |
TR style= "margin-left:210px;" >
| cache n bytes |
| buffering = negative |
using system defaults, Depending on the system, |
The return value of the <4>open () function is a file object, which I understand is a pointer to the first address in memory.
2. Closing the file (built-in function) close ()
For security, as well as specification, after you open the file using finish, be sure to use close () to close the file.
>>> data_file = "E:/tmp/data.txt"
>>> f = open (data_file, ' RT ')
>>> here is the specific operation of the file object
>>>f.close ()
3. Read operation of the file
The open () function returns a file Objec, and the operation described here is the action associated with the Document object
<1>file.next ()
Returns the character between the file pointer position and the nearest line break ' \ n ' (contains the character that the file refers to and the newline character). Where file refers, there is no limit to the starting position of a row.
<2>file.read ([size])
Reads up to size bytes starting at the point where the file is located, meaning that if you do not have enough data to read the size of a byte, you have encountered the end-of-document EOF.
Read to return the content that is currently being read.
* Here is a small problem, that is, after using the next () method, if using read (size) will be wrong, there is no reason to know.
<3>file.readline ([size])
The size parameter returns a whole row of data, by default;
Given the size parameter, returns the data in the row containing the file, starting at the file up to size bytes, which is the actual return data size = min{The data size after the row file, size}
<4>file.readlines ([size])
Size by default, all rows of the file are read and stored in a list object.
4. File Write operations
Before you write a file, make sure that the file is opened in a writable manner.
<1>file.write (str)
To write the target string str to a file, it is important to note that because of the caching mechanism, the STR content may not actually be written to the file until the file is closed. So, remember the close () operation of the file.
<2>file.writelines (Sequence)
You can iterate over the contents of the object sequence and write to the file in turn. Often used in conjunction with ReadLines ().
5. File pointers
<1>file.tell () returns the current position of the file pointer
<2>file.seek (Offset[,whence]) offset: offset; whence: relative position
| Whence |
Offset |
| 0 (beginning of file) |
>=0 integer, if the negative number will be an error |
| 1 (file pointer current position) |
If it crosses the border, it will error |
| 2 (end of file) |
Generally with negative numbers (using a positive number is not an error, do not know why) |
The mode parameter, which is read-only by default
Python File Operations Summary