I. Document processing process
Character encoding: Compiling a person's understanding of a character into a number that a machine can recognize
Two. Basic operation
1. Two modes of reading
(1) F=open (' Test.txt ', ' R ', Encoding=utf-8) #应用程序打开F, operating system open open file, default read mode, character encoding is UTF-8, read mode in file does not exist is unable to create file, error
Print (F.read ()
F.close ()
(2) F=open (' Test.txt ', ' RB ') #以byte模式读取文件
Print (F.read (' Hello ', Enceod (' UTF-8 '))
2. Various file operations
' R ' mode, read only, not present will error
' W ' mode, write only, does not exist will be created, presence will overwrite
' A ' mode, append, not exist will be created, exist then append at the end of the file, the cursor jumps to the last
F.read () #默认读取所有, f.readable, determines whether it is readable, f.readline () reads by line, F.readlines () reads all at once, and puts it in the list.
F.tell () viewing the cursor position
With open (' Test.txt ', ' W ', encoding= ' UTF-8 ') as F:
F.write () #打开文件赋值给f, performs a write.
Cursor movement of the file:
Read (3), when the file is opened as text, reads 3 characters, opens mode to B mode, reads 3 bytes
Seek three ways to move:
Seek (0), 0 mode moves to the beginning of the file by default, with the beginning as the reference bit by default
Seek (3,1), 1 mode is based on the position of the current cursor as a reference, 3 bytes after the move, only in B mode use
Seek ( -3,2), 2 mode defaults to the end of the file as a reference, the forward-cut cursor
1, move end of file
Python path-File handling