Initial knowledge of file operation
-
Using python code to write a very low software to operate files
- File path: Path
- Open mode: read, write, append, read write, write read
- Encoding mode: utf-8, gbk, gb2313
-
'' Trilogy
1. Open file
2. Perform corresponding operations on the file handle
3. Close file
''' -
Use the open() function to ensure that the file object is closed, that is, call the close() function.
-
Problem caused by path separator: add r before the problem \ or add another\
-
Encoding = it's not a specific encoding or decoding, it's just a statement: what codebook is used to open the file this time. Generally speaking, you can open your files in whatever way you want to save them in utf-8 (some use gbk).
-
Mode: optional, file open mode (can not write, the default is mode = 'r')
- r: Open the file read-only. The pointer to the file will be placed at the beginning of the file. This is the default mode.
-
Open(): built in function. The open bottom layer calls the interface of the operating system.
f1 = open('F:\Ziqing.txt',encoding='utf-8',mode='r') content = f1.read() print(content) #content is a string type f1.close() ''' //Built in function, the open bottom layer calls the interface of the operating system. f1,Variable. f1,fh,file_handler,f_h,File handle: any operation on a file must be done through the file handle.How. encoding: Can not write (must write), do not write parameter default codebook: the default codebook of your operating system Windows: gbk Linux: utf-8 mac: utf-8 read: Read it all out f1.close(): Close the file handle (if it is not closed, the content will be occupied all the time, and you should remember to close it every time you run out) '''
Reading of file operation
-
r. R B, r+,r+b... Four modes () each with the following methods
Under the same directory or folder, find the file and enter the file name directly (relative path)
-
r:
-
Read(): read all
f_h = open('Reading of documents',encoding='utf-8',mode='r') content = f_h.read() print(content) f_h.close()
-
Read (if you want to read a few characters, write a few): read according to the characters (if you want to write a few characters, read a few characters and read a few spaces, it also counts)
f_h = open('Reading of documents',encoding='utf-8',mode='r') content = f_h.read(4) print(content) f_h.close()
-
readline(): read by line
f_h = open('Reading of documents',encoding='utf-8',mode='r') print(f_h.readline()) f_h.close()
-
readlines(): returns a list. Each element in the list is a line of the source file.
f_h = open('Reading of documents',encoding='utf-8',mode='r') l1 = f_h.readlines() print(l1) f_h.close() #same f_h = open('Reading of documents',encoding='utf-8',mode='r') print(f_h.readlines()) f_h.close()
-
for loop read
f_h = open('Reading of documents',encoding='utf-8',mode='r') for i in f_h: print(i) f_h.close() #The effect is the same, but if it is a large file, you must loop the file handle, not the readLines f_h = open('Reading of documents',encoding='utf-8',mode='r') l1 = f_h.readlines() for i in l1: print(i) f_h.close()
-
-
rb: non text files are manipulated. Picture, video, audio. (rb mode does not use encoding)
f = open('Cai Xukun.jpg',mode='rb') content = f.read() print(content) f.close() #bytes type
-
Write file operation
w. Four modes of W B, W +, w+b
-
w: No file, create file, write content (if the file exists, empty the content of the source file before writing new content)
- When writing a file operation, if only the file name is written and no directory is written, the file will be created in the current directory, that is, the same folder by default
- Empty problem: close the file handle and open the file in w mode again
#Create to the same directory f = open('Writing of documents',encoding='utf-8',mode='w') f.write('Ziqing loves learning') f.close() #Ziqing loves learning f = open('Writing of documents',encoding='utf-8',mode='w') f.write('Someone's trying to steal where you can't see them') f.close() #Clear before writing #Someone's trying to steal where you can't see them
-
wb: non text files are also operated on (text files can also be operated on) (also without encoding)
#Read this picture first #Then use the read bytes. Write new file f = open('Cai Xukun.jpg',mode='rb') content = f.read() f.close() fh = open('Hyk .jpg',mode='wb') fh.write(content) fh.close()
Append of file operation
a. Four modes of a B, a +, a+b
-
a: There is no file to create, append content (if there is this file, append content on the last side of the source file)
#Without this file, create and append f1 = open('Appending of files',encoding='utf-8',mode='a') f1.write('I have a great sense of achievement in my study') f1.close() #With this file, append it after the source file f1 = open('Appending of files',encoding='utf-8',mode='a') f1.write('Oh, oh, oh, oh') f1.close()
Other modes of file operation
-
r +: (read / write) can't create a new file. Note: read first and write later (in fact, read first and then add content at the end, which can be said to be read and add) (Note: if you write first and then read in read-write mode, then the file will have problems, because the default cursor is at the beginning of the file. If you write first, the content written will cover the original content, straight To overwrite what you've written, and then read it later.)
f = open('Reading and writing of file operation',encoding='utf-8',mode='r+') content = f.read() print(content) f.write('at sixes and sevens') f.close()
Other functions of file operation (important)
f.***()
- tell(): get the cursor position in bytes
- seek(): adjust the cursor position. The unit is bytes. By default, the cursor starts from scratch, that is, seek(0). We can use this to adjust the cursor position and choose where to start reading. seek(0,2) is the last adjustment to the file
- flush(): force refresh (you must follow flush after writing, because some will not help you save, flush will help you force save)
Another way to open a file
With: with indentation, you can operate multiple files (write on the same line, or use \ to wrap the line, indicating that this line is not finished and the next line is the same line)
-
Advantage: no need to close the handle manually (it will disappear in a certain period of time).
- Multiple files can be operated
with open('Reading of documents',encoding='utf-8',mode='r') as f1: print(f1.read())
with open('Reading of documents',encoding='utf-8',mode='r') as f1,\ open('Writing of documents',encoding='utf-8',mode='w') as f2: print(f1.read()) f2.write('ziqing')
Modification of documents
low version
-
Open the original file in read mode
-
Create a new file in write mode
-
Read out and modify the contents of the original file into new contents and write them into a new file
-
Delete source file
-
Rename the new file to the original
import os with open('Reading of documents',encoding='utf-8',mode='r') as f1,\ open('Reading of documents 2',encoding='utf-8',mode='w') as f2: old_content = f1.read() new_content = old_content.replace('Ziqing','handsome guy') f2.write(new_content) os.remove('Reading of documents') os.rename('Reading of documents 2','Reading of documents')
Upgraded version (low version is directly read out with read. If the file is large, it will directly burst the memory, so here use for loop, line by line operation)
import os with open('Reading of documents',encoding='utf-8',mode='r') as f1,\ open('Reading of documents 2',encoding='utf-8',mode='w') as f2: for i in f1: new_content = i.replace('handsome guy','Han Ziqing') f2.write(new_content) os.remove('Reading of documents') os.rename('Reading of documents 2','Reading of documents')
summary
Three main directions:
- Reading, four modes: r, r B, r +, r+b
- Write, four modes: w, w B, w +, w+b
- Added, four modes: A, a B, a +, a+b
Corresponding functions: operation on file handle: read(), read(n), readline(), readlines(), write(), tell(), seek(), flush()
Operation of document modification (key)