One. Open File
f = open ("DB", "R", encoding= "UTF8")
- R: Read-only
- W: Write only, the file will be emptied first
- A: Append
- X: If file exists, then error, otherwise, create file and write only
- B: The above parameters can be added "B", meaning to open the file in binary form, read writes are bytes, there is no need to consider "encoding"
- Encoding= "UTF8": tells the Python interpreter to transcode with UTF8, and when we read the string The interpreter converts the byte into a string, and when we write the string The interpreter automatically converts the string to bytes, which is done by the Python interpreter. We just need to make sure that the encoding is consistent when the file is opened and when the file is saved.
- +:r+,w+,a+,x+ are read and write, but we generally use r+, because, w+ will first empty the file, A + is appended, x+ file exists will be error
- With open ("FileName", "Open with", encoding= "UTF8") as F: This file is recommended to open without closing the file and can open two files simultaneously, as follows: with open ("db", "R", encoding= "UTF8") as F1, open ("DB2", "W", encoding= "UTF8") as F2
Example: Opening a file with "B"
1 #Open file with b2With open ("DB","WB") as F:3Temp=bytes ("Zhang Yan", encoding="UTF8")4 Print(temp)5 f.write (temp)6With open ("DB","RB") as F:7 Print(F.read ())8 9 Execution Result:TenB'\xe5\xbc\xa0\xe7\x87\x95' OneB'\xe5\xbc\xa0\xe7\x87\x95'Open file with b
Two. Operating files
- Read (): No parameters, read all; parameter (n), file open plus b read n bytes, no b words read n characters
- Tell (): Gets the position of the current pointer (in bytes)
- Seek (): pointer jumps to the specified position (bytes)
- Write (): Writes the data, the file opens plus B, the writing section, and no B words, write characters
- Fileno (): File descriptor
- Flush (): From memory brush to hard disk
- Readable (): File readable return true, otherwise false
- ReadLine (): reads a row
- Truncate (): Truncate the file, will delete the contents after the pointer
- For lines in F: Loop file per line
- F.close (): Close file
1 #seek is unaffected by encoding, Seek is byte-by-bit, and if you have Chinese, seek (1) will split this Chinese2With open ("DB","r+", encoding="UTF8") as F:3F.seek (3)4F.write ("Flag")5 6 perform the pre-DB content:7 AB Zhang Yan ijklmnopqrstuvwxyz Zhang Jie8 post-Execution db content:9 ab?flag?ijklmnopqrstuvwxyz Zhang JieTen One #Tell () Gets the position of the current pointer, as well as seek, in bytes . AWith open ("DB","r+", encoding="UTF8") as F: - Print(F.read ()) -With open ("DB","r+", encoding="UTF8") as F: the Print(F.read (2)) - Print(F.tell ()) - - Execution Result: + Zhang Yan abcdefghijklmnopqrstuvwxyz - Zhang Yan +6Seek and tell
1 #F.write () writes from the pointer position, if the pointer overwrites the corresponding byte in the middle of the file2 #However, if you start reading a file, whether or not you read the end of the file, the Python rules will start at the end of the file3With open ("DB","r+", encoding="UTF8") as F:4F.seek (2)5F.write ("Zhang Yan")6 F.seek (0) # actively move the pointer to the beginning of the file7data = F.read (12)8 Print(data)9F.write ("Zhang Jie")Ten f.seek (0) Onedata =F.read () A Print(data) - - Execution Result: the ab Zhang Yan Ijklmnop -Ab Zhang Yan ijklmnopqrstuvwxyz Zhang JieWrite
1 #f.truncate ()2 #truncate the file, it will delete all the contents after the pointer3With open ("DB","r+", encoding="UTF8") as F:4 Print(F.read ())5F.seek (3)6 f.truncate ()7 f.seek (0)8 Print(F.read ())9 Ten Execution Result: One1234567890 A123truncate
Python growth path 9--file operation Open