File operations
1. Initial knowledge of file operation
2. Read-only (R,RB)
3. Write only (W,WB)
4. Append (A,ab)
5.r+ Reading and writing
6.w+ Write Read
7.a+ Write read (append write read)
8. Other ways of writing
9. File modification and another way to open a file handle
Main content:
1. We can open a handle to a file by using the opening () function
How to open a file: R,w,a,r+,w+,a+,rb,wb,ab,r+b,w+b,a+b, using R (read-only mode) by default
2. Read-only (R and RB) operation
# r Mode f = Open (' H:/123.txt ', mode= ' R ', encoding= ' UTF-8 ') # opens 123 files, in a read-only manner, the encoding format is utf-8centent = F.read () # Read the file print ( centent) # Output Content f.close () # Close File # RB Mode f = open (' H:/123.txt ', mode= ' RB ') # Open 123 file, with read-only byte centent = F.read () # reads the file and reads out the print (centent) # Output of the byte pattern . Output byte form of F.close () # Close File
Absolute path and relative path:
Absolute path: From the root directory down to the location of your file
Relative path: Based on the path of your current location
.. / return to the previous level
How to read the file:
Read (): all the contents of the file read out, the disadvantage: the memory, if the file is too general collapse
Read (n): reads several characters and reads several bytes in RB mode
ReadLine (): reads one line at a time, after each read of the data there is a \ n, need to remove
readlines (): Each line is formed into an element and placed in a list
It's best to use a for loop to open
Be sure to f.close ()to close the handle after reading it
How to read a file (W,WB):
When writing, note that if there is no file, the file will be created, and if the file exists, the contents of the file will be deleted and the new content written.
Write (): writes content that is unreadable in the mode of writing to the content
flush (): Refresh File
In WB mode, you can not specify the encoding of the open file, but you must convert the string to UTF-8 bytes data at the time of writing.
encode (encoded): Character is encoded as a self-section
decode (encoded): bytes are decoded into characters
Append (A,ab)
In Append mode. What we write will append to the end of the file
Read/write mode (r+, r+b)
For read-write mode. Must be read first. Because the default cursor is at the beginning of the. Ready to read. When you are finished reading, write again. The most frequently used pattern we'll ever use is r+.
Need to read first in writing
Write Read (w+, w+b)
Clear all the contents first. and then write. Last read. But the read content is empty, not commonly used
Additional read (A +)
A + mode, whether read first or after. Data is not read.
Other operations:
Seek (N): Seek (n) move the cursor to n position, note that the unit of movement is byte. So if the Chinese part of UTF-8 is a multiple of 3. Usually we use seek to move to the beginning or the end. Move to the beginning: Seek (0) move to the end: Seek (0,2) the second parameter of seek indicates where the offset is from, the default is 0, the start, 1 means the current position, and 2 means the end
Tell (): Use Tell () to help us get the current cursor position
truncate () truncate file
In r+ mode. If the content is read. No matter how much content is read. How much the cursor is showing. When you write to or manipulate the file, it is done at the end. So if you want to do the truncation operation. Remember it. To nudge the cursor first. Move to the position you want to truncate. Then truncate about truncate (n), if given N. Is truncated from the beginning, and if n is not given, it is truncated from the current position. Subsequent content will be deleted.
Another way to open a file:
with open ("Doll", mode= "R", encoding= "Utf-8") as F1:
This does not need to close the file, using the same way as above
Python Full Stack development 08