Files are commonly used in daily programming, typically for storing data or application-system parameters
Grammar:
Open (Filename,mode)
Parameters:
FileName: The file name to access
Mode: Open the file
First, about open mode
Mode |
Description |
R |
Open the file as read-only. The pointer to the file will be placed at the beginning of the file. This is the default mode. |
W |
Open a file for writing only. Overwrite the file if it already exists. If the file does not exist, create a new file. |
A |
Opens a file for appending. If the file already exists, the file pointer will be placed at the end of the file. In other words, the new content will be written to the existing content. If the file does not exist, create a new file to write to. |
Rb |
Opens a file in binary format for read-only. The file pointer will be placed at the beginning of the file. This is the default mode. |
Wb |
Open a file in binary format only for writing. Overwrite the file if it already exists. If the file does not exist, create a new file. |
Ab |
Opens a file in binary format for appending. If the file already exists, the file pointer will be placed at the end of the file. In other words, the new content will be written to the existing content. If the file does not exist, create a new file to write to. |
r+ |
Open a file for read-write. The file pointer will be placed at the beginning of the file. |
w+ |
Open a file for read-write. Overwrite the file if it already exists. If the file does not exist, create a new file. |
A + |
Open a file for read-write. If the file already exists, the file pointer will be placed at the end of the file. The file opens with an append mode. If the file does not exist, create a new file to read and write. |
rb+ |
Opens a file in binary format for read-write. The file pointer will be placed at the beginning of the file. |
wb+ |
Opens a file in binary format for read-write. Overwrite the file if it already exists. If the file does not exist, create a new file. |
ab+ |
Opens a file in binary format for appending. If the file already exists, the file pointer will be placed at the end of the file. If the file does not exist, create a new file to read and write. |
1. Read mode
Read mode R, opens the file as read-only, and the pointer to the file is placed at the beginning of the file. This is the default mode
f = open (' Test.txt ', ' R ', encoding= ' UTF-8 ') #只读方式, encoding is the meaning of transcoding, telling the interpreter to encode the UTF-8 format i = f.read () #读取文件, and assigned to IPrint (i) f.close () #关闭文件
2. Write mode
Write mode W, open a file for writing only. Overwrite if the file already exists, and if the file does not exist, create a new file
f = open (' Test.txt ', ' W ', encoding= ' UTF-8 ') f.write (' \ n This is write ') F.close ()
3. Append mode
Append a opens a file for appending. If the file already exists, the file pointer will be placed at the end of the file, and if the file does not exist, create a new file to write to
f = open (' Test.txt ', ' a ', encoding= ' UTF-8 ') f.write (' This is write ') #写入print ("cursor position before positioning:%s"% (F.tell ())) F.flush () #使内存的内容刷新至文件f. Seek (0) #因为W + after reading the file will be positioned at the end of the file, so you need to reposition the cursor position print ("cursor position after positioning:%s"% (F.tell ())) F.close ()
4. Read/write mode
Read-write mode r+, open a file for read and write. The file pointer will be placed at the beginning of the file
f = open (' Test.txt ', ' r+ ', encoding= ' UTF-8 ') #读写方式i = F.read () print (i) f.write (' \ n This is write ') #写入f. Close ()
5. Read/write mode
Read-write mode w+, open a file for read and write. Overwrite the file if it already exists. If the file does not exist, create a new file
f = open (' Test.txt ', ' w+ ', encoding= ' UTF-8 ') f.write (' This is write ') #写入print ("cursor position before positioning:%s"% (F.tell ())) F.flush () #使内存的内容刷新至文件f. Seek (0) #因为W + after reading the file will be positioned at the end of the file, so you need to reposition the cursor position print ("cursor position after positioning:%s"% (F.tell ())) i = F.read () print (i) F.close ()
6. Read/write mode
Read A + to open a file for reading and writing. If the file already exists, the file pointer will be placed at the end of the file and will be appended when the file is opened. If the file does not exist, create a new file to read and write
f = open (' Test.txt ', ' A + ', encoding= ' UTF-8 ') f.write (' This is write ') #写入print ("cursor position before positioning:%s"% (F.tell ())) F.flush () #使内存的内容刷新至文件f. Seek (0) #因为W + after reading the file will be positioned at the end of the file, so you need to reposition the cursor position print ("cursor position after positioning:%s"% (F.tell ())) i = F.read () pri NT (i) F.close ()
Second, the commonly used functions
Serial number |
Method and Description |
1 |
File.close () Close the file. The file can no longer read and write after closing. |
2 |
File.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. |
3 |
File.fileno () Returns an integer that is a file descriptor (Descriptor FD Integer) that can be used in some of the underlying operations, such as the Read method of the OS module. |
4 |
File.isatty () Returns True if the file is connected to an end device, otherwise False is returned. |
5 |
File.next () Returns the next line of the file. |
6 |
File.read ([size]) Reads the specified number of bytes from the file, if none is given or is negative. |
7 |
File.readline ([size]) Reads the entire line, including the "\ n" character. |
8 |
File.readlines ([Sizeint]) Reads all rows and returns a list, and if given sizeint>0, returns a row with a sum of approximately sizeint bytes, the actual read value may be larger than sizeint because the buffer needs to be populated. |
9 |
File.seek (offset[, whence]) Set the current location of the file |
10 |
File.tell () Returns the current location of the file. |
11 |
File.truncate ([size]) Truncates from the beginning of the first line of the file, truncates the file to size characters, no size indicates truncation from the current position, and all characters after the truncation are deleted, where the line break under the Widnows system represents a 2-character size. |
12 |
File.write (str) Writes a string to the file without a return value. |
13 |
File.writelines (Sequence) Writes a list of sequence strings to a file, and adds a newline to each line if a line break is required |
Python file Processing