First, the document processing process
- Open the file, get the file handle and assign a value to a variable
- Manipulating a file with a handle
- Close File
Second, the File open mode
When you open a file, you need to specify the file path and how you want to open the file, and then open it to get the file handle and manipulate it later through the file handle.
The mode of opening the file is:
- R, read-only mode "default mode, file must exist, not present, throw exception"
- W, write-only mode "unreadable; not exist" created; empty content "
- X, write-only mode "unreadable; not present, create, present error"
- A, append mode "readable; not present, create; append content only"
"+" means you can read and write a file at the same time
- r+, read and write "readable, writable"
- w+, write "readable, writable"
- x+, write "readable, writable"
- A +, write "readable, writable"
"B" means to operate in bytes
- RB or R+b
- WB or W+b
- XB or W+b
- AB or A+b
Note: When opened in B, the content read is a byte type, and a byte type is required for writing, and encoding cannot be specified
| 123456789101112131415 |
#r模式,默认模式,文不存在则报错1) f=open(‘a.txt‘,encoding=‘utf-8‘) print(‘first-read:‘,f.read()) #读文件所有内容 print(‘seconde-read: ‘,f.read()) #光标在最后一行,第二个打印读不出内容2) f=open(‘a.txt‘,encoding=‘utf-8‘) print(f.readline(),end=‘‘) #读文件一行 print(f.readline(),end=‘‘) print(f.readlines()) #读出文件所有行 f.close() #关闭文件 |
| 123456789 |
#w模式,文不存在则创建,文件存在则覆盖f=open(‘a.txt‘,‘w‘,encoding=‘utf-8‘)f.write(‘1111111\n22222\n3333\n‘) #\n是换行#也可以这样写f.writelines([‘111hhh\n‘,‘222aa2\n‘,‘3333\n‘])f.close() |
| 12345 |
#a模式,文不存在则创建,文件存在不会覆盖,写内容是追加的方式写 f=open(‘a.txt‘,‘a‘,encoding=‘utf-8‘) f.write(‘\n444444\n‘) f.write(‘5555555\n‘) f.close() |
| 1234567 |
#其他方法# f=open(‘a.txt‘,‘w‘,encoding=‘utf-8‘)# f.write(‘asdfasdf‘)# f.flush() #把内存数据刷到硬盘# f.close()# print(f.closed) #判断文件是否关闭# f.readlines() |
Third, the file built-in function flush
Flush principle:
- File operation is the software to read files from the hard disk to memory
- The operation of writing to the file is also stored in buffer buffers (memory speed faster than the hard disk, if the data written to the file from memory to the hard disk, memory and hard disk speed delay will be infinitely amplified, inefficient, so to brush the data to the hard disk we unified into the memory of a small piece of space, buffer, After some time the operating system will flash the data in buffer to the hard disk.
- Flush that is, forcing the written data to be brushed to the hard disk
Iv. an explanation of the open function
1. Open () syntax
Open (file[, mode[, buffering[, encoding[, errors[, newline[, Closefd=true] []] ])
The Open function has a number of parameters, commonly used file,mode and encoding
file files, need to be quoted
mode File open mode, See the following 3
buffering for 0,1,>1 three, 0 for buffer off (binary mode only), and 1 for line buffer (text mode only),> 1 indicates the buffer size of the initialization;
encoding indicates what encoding is used for the returned data, generally UTF8 or GBK;
Errors The value is generally strict,ignore, when taking strict, character encoding problems, will error, when taking ignore, coding problems, the program will be ignored, continue to execute the following program.
NewLine can take a value of None, \ n, \ r, ", ' \ r \ n ', to differentiate between newline characters, but this parameter is valid only for text mode;
closefd , is related to the file parameters passed in, by default, True, the file parameter passed to the file name, the value is false, file can only be a document descriptor, what is a file descriptor, is a non-negative integer, in the Unix kernel system, open a file, A file descriptor is returned.
2. the difference between file () and open () in Python
Both can open the file, the operation of the file, but also have similar usage and parameters, but, the two file open way there is an essential difference, filefor the document class , the file () to open files , equivalent to this is in the construction of the file class, and open () Opening the file is done using Python's built-in functions , and it is recommended to use the Open
V. Context Management
| 123456789101112 |
#上下文管理with# read_f=open(‘a.txt‘,‘r‘,encoding=‘utf-8‘)# write_f=open(‘.a.txt.swp‘,‘w‘,encoding=‘utf-8‘)#上面两句可以缩写成下面一句with open(‘a.txt‘,‘r‘,encoding=‘utf-8‘) as read_f,\ open(‘.a.txt.swp‘,‘w‘,encoding=‘utf-8‘) as write_f: for line inread_f: if‘alex‘inline: line=line.replace(‘alex‘,‘ALEXSB‘) write_f.write(line)os.remove(‘a.txt‘)os.rename(‘.a.txt.swp‘,‘a.txt‘) |
Python (day5) file operation