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
II. Basic procedures for document processing
f = open (' chenli.txt ') #打开文件first_line = F.readline () print (' first line: ', first_line) #读一行data = F.read () # reads all the rest of the content, Do not use print (data) #打印读取内容 F.close () when the file is large #关闭文件
File code:
Note: When you save the file, you should be aware of how the file is encoded, the Python interpreter default encoding when no open encoding is specified, python2.* is ascii,python3.* to Utf-8
F=open (' Chenli.txt ', encoding= ' GBK ') #指定编码方式f. Read ()
File Open mode:
File handle = open (' File path ', ' 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.
built-in functions for files 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
Scroll bar:
Import sys,timefor I in range (101): s= "\r%d%%%s"% (I, "#" *i) Sys.stdout.write (s) Sys.stdout.flush () Time.sleep (0.1) #设置时间29% ############################ #64% ####################################################### ######## #100% ################################################################################################## ##
Cursor movement within the file: Note: Read (3) represents the reading of 3 characters, and the rest of the files within the cursor movement are in bytes as in Seek,tell,read,truncate finishing.
The Open function is detailed:
1. Open () Syntax
Open (file[, mode[, buffering[, encoding[, errors[, newline[, Closefd=true] []]])
The Open function has many parameters, Commonly used are File,mode and encoding
file location, need to quote
mode File open mode, see below 3
The buffering values are 0,1,>1 three, 0 for buffer off (binary mode only), 1 for line buffer (text mode only), and >1 for the initialized buffer size;
encoding indicates what encoding is used for the returned data, generally UTF8 or GBK;
errors values generally have strict, Ignore, when taking strict, when the 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 value, is related to the incoming file parameters, 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 a Unix kernel system, a file descriptor is returned by opening it.
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 the construction of the file class, and open () opened the file, is used Python's built-in functions , it is recommended to use the Open
3. Basic values of the parameter mode
R, W, A is the basic mode of open file, corresponding to read-only, write-only, append mode;
B, T, +, u these four characters, with the above file open mode combination, binary mode, text mode, read and write mode, universal line break, according to the actual situation combination of use,
Common mode value combinations:
R or RT default mode, text mode read RB binary file w or wt text mode write, open before file storage is emptied WB binary write, file storage is also emptied a append mode, can only be written at the end of the file A + read-write mode, Write can only be written at the end of the file w+ can read and write, and A + is the difference is to clear the contents of the file r+ can read and write, and A + difference is can be written to the file anywhere
Python-based file processing