Open File
Parameter description:
File: path to files
Mode: file read and write mode, default ' R ', read-only mode;
Buffering: Set buffer policy, 0 for binary file, 1 for row buffer, for text mode; default binary fixed size buffer, text file row buffer
Encoding: Set encoding, default utf-8; This parameter cannot be used in binary mode;
Errors: Set How to handle the encoding exception of the file, default strict, error throws an exception, set ignore ignore encoding exception, may result in data loss, this parameter cannot be used in binary mode;
NewLine: Set line break, the default newline character is ' \ n ', ' \ R ', ' \ r \ n ', when writing to a file, all of the above three of the text will be converted to ' \ n ' newline character, and when set to ', the default mode is also enabled;
Closefd=true: Sets the state of the file descriptor, when false, the file is closed but the descriptor does not close, but the file name is specified when you open it, then setting false does not work.
How files are opened
r: 以只读方式打开文件。文件的指针将会放在文件的开头。这是默认模式。w: 打开一个文件只用于写入。如果该文件已存在则将其覆盖。如果该文件不存在,创建新文件。a: 打开一个文件用于追加。如果该文件已存在,文件指针将会放在文件的结尾。也就是说,新的内容将会被写入到已有内容之后。如果该文件不存在,创建新文件进行写入。x: 创建一个新文件,将其打开并编写;如果文件已经存在报错;rb: 以二进制格式打开一个文件用于只读。文件指针将会放在文件的开头。这是默认模式。wb: 以二进制格式打开一个文件只用于写入。如果该文件已存在则将其覆盖。如果该文件不存在,创建新文件。ab: 以二进制格式打开一个文件用于追加。如果该文件已存在,文件指针将会放在文件的结尾。也就是说,新的内容将会被写入到已有内容之后。如果该文件不存在,创建新文件进行写入。r+: 打开一个文件用于读写。文件指针将会放在文件的开头。w+: 打开一个文件用于读写。如果该文件已存在则将其覆盖。如果该文件不存在,创建新文件。a+: 打开一个文件用于读写。如果该文件已存在,文件指针将会放在文件的结尾。文件打开时会是追加模式。如果该文件不存在,创建新文件用于读写。rb+:以二进制格式打开一个文件用于读写。文件指针将会放在文件的开头。wb+:以二进制格式打开一个文件用于读写。如果该文件已存在则将其覆盖。如果该文件不存在,创建新文件。ab+:以二进制格式打开一个文件用于追加。如果该文件已存在,文件指针将会放在文件的结尾。如果该文件不存在,创建新文件用于读写。
Close File
=open(file)f.close()
Attention:
Because the file needs to be closed every time it is opened, you can use the context Manager to manage the file, which automatically closes the file at the appropriate time.
withopenas f: f.read()
File General read/write
Method One: Read ()
with open(filename) as f: f.read() # 一次性读取全部,在大文件这是不可取的 f.read(5) # 读取5个字节,如果使用UTF-8编码,5表示5个汉字,也表示5个字母。 f.read(6) # 同一个f对象,多次读取时在上一次的基础上继续往下,如果超过则有多少读多少
Method Two: ReadLine ()
with open(filename) as f: f.readline() # 按行来读取,每次返回一行, f.readline() # 多次读取在上一行的基础上往下,换行符也会被读取
Method Three: ReadLines ()
with open(filename) as f: t = f.readlines() # 按行读取全部的内容作为一个列表返回
Method One: Write ()
with open(filename,‘w‘,encoding=‘utf-8‘) as f: t = f.write(‘ddd‘) # 将内容写入,必须是字符串格式,不可以是数字,返回的是字符串的个数,包括了换行符\r\n占两个字符(windows),占一个字符\n(linux)。 t = f.write(‘aaa‘) # 多次写入在原来的基础上继续写入
Method Two: Writelines ()
with open(filename,‘w‘,encoding=‘utf-8‘) as f: f.writelines([‘aa‘,‘bb‘]) # 该方法自动将列表元素拼接起来写入文件,参数时一个可迭代对象,列表、字典、集合都可以
- Writeable () can determine whether an open file object F is readable or writable.
The problem of attention
F.write (123) This is not allowed and must be converted into binary or string;
Open (filename, ' RB ', encoding= ' Utf-8 '): Opening a file in binary format is not allowed to specify the encoding, otherwise error
To open a file in binary format, the write must be a byte file, while the write returns the number of bytes, open in text mode, write must be text, and return the number of strings, a Chinese character string accounted for 3 bytes.
- Methods for converting ordinary strings to byte files:
B ' 123ADF ': This method can only be used for numbers and letters, the default ASCII encoding, can not convert Chinese characters;
' 123ADF '. Encode (' Utf-8): can convert letters and kanji;
Bytes (' My ', encoding= ' Utf-8 '): can convert letters and kanji, but must specify encoding format;
Locating and reading files
- Tell (): Gets the position of the file pointer
withopen(filename,‘r‘,encoding=‘utf-8‘as f: f.read(3) = f.tell() # 获取指针的位置,返回3,如果读取到换行符,换行符在windows占两个字节
- Seek (Offset,from): Sets the position of the pointer
Parameters
- Offset: Offsets, set negative numbers for forward offsets, positive numbers for backward offsets, one kanji for 3 bytes, and one byte for letters
- From: The direction of the offset
0:表示文件开头1:表示当前位置2:表示文件末尾
withopen(filename,‘r‘,encoding=‘utf-8‘as f: f.read(3) = f.seek(0,0# 返回指针定位后的实际位置,默认偏移方向从文件开头算起
Attention
Open the file in text mode, the offset direction only from the beginning of the file, the parameter from only 0, this is because the text pattern involves coding problems, in binary format to open the file can be set in different directions.
Each time the file is opened, the pointer to the read operation is at the beginning of the file, and the write operation pointer is at the end of the file until the file is closed;
A few classic cases
- The simultaneous reading and writing of files
with Open ( ' test.txt ' , ' r+ ' ) as f: print (F.tell ()) # the position of the current pointer at the beginning 0 a = f.read (3 ) # pointer to position 3 print (a) prin T (F.tell ()) H = f.write ( ' HHH ' ) # written from the end of the file, Equivalent to append data print (h) C = F.tell () # pointer in End of file print (c) print (F.read ()) # read Data from position 3 to the end of the original file print (F.tell ()) # the current position at the end of the new file and C same pre>
Summary: In r+ mode, if the same file object needs to read and write, read and write each have a pointer, they are independent of each other, read and write positions are not disturbed; but the tell function gets the position of the first write, that is, when the program executes, if only read, tell gets the location of the read; , tell the location of the acquisition must be the location of the write, no matter the back there is no more reading operations, the same as other patterns;
File read and write at the same time there are offsets
with Open(' Test.txt ',' r+ ') asF:Print(F.tell ())# The position of the current read pointer is at the beginning of 0, and the write pointer is not initialized Print(F.read (3))# Read the pointer to position 3 Print(F.tell ())# Gets the position of the read pointer to 3F.seek (0)# The pointer shifts to the beginning of the file and affects only one pointer Print(F.read (3))# Read the pointer to position 3H=F.write (' GGGG ')# Add a file descriptor, write pointer initialized to the end of the file, write the dataJ=F.tell ()# Get write position first, write pointer at end of file Print(j) F.seek (0)# pointer offset to the beginning of the file, read the data update of the file, add the written dataJ=F.tell ()# Gets the write pointer at the beginning of the file Print(j) H=F.write (' VVVV ')# Write data from the beginning of the file, overwriting the original dataC=F.tell ()# Get the Write pointer at the location of the file 4 Print(c)Print(F.read (4))# 4 characters to read from position 4 Print(F.tell ())# Read pointer in file 8 locationH=F.write (' xxx ')# writes from the end of the file, equivalent to append data Print(F.tell ())# Current position at end of new file Print(F.read ())# Read all data from a 8-bit position
The Seek () function can only affect the first read and write operation under it, and the last written data is updated to the read buffer;
In r+ mode, write () is always written from the end of the file, unless it is affected by the Seek function;
After the write () operation is offset, a pointer to the read operation is moved to the position of the write operation;
- A file that reads and writes at the same time and has updates
withopen(‘test.txt‘‘r+‘as f: f.read() f.seek(2,0) # 定位到到文件的开头往右2的位置 f.truncate() # 将2位置后所有的数据删除 f.write(‘aaa‘) # 写入新的数据
- Truncate (): Delete data dynamically, delete all subsequent data from the current pointer
Other methods
Flush (): Refreshes the internal buffer of the file, directly writes the data of the internal buffer to the file immediately, instead of passively waiting for the output buffer to be written, the general close () function calls Flush () before closing the file descriptor.
Fileno: Returns an integer file descriptor that is seldom used;
Isatty (): Whether the file is connected to a device on a terminal;
Truncate (): File interception, no parameters are removed from the current pointer position after all, there are parameters when the first letter from the beginning of the interception of data left, the rest of the deletion;
Python file and read-write explanation