I. Documentation operations
1. The concept of documentation
1. File is the computer exposed to the user to operate the hard disk shortcuts
2. A computer file is data that is used to store some kind of information
3. In computers, files store information in binary form
2. The way the code opens the file
Mode 1: f = open(file path, read-write mode, encoding='utf8') () Mode 2: # The close method is automatically called to close the resource after the subcode has finished running with open(file path, read-write mode,encoding='utf8') as f: prerequisites: # 1, the first parameter of the open method is the path of the file, and crowbar with a combination of some letters will produce a special meaning of the path to find a confusing In order to solve the problem you can add the letter r in front of the path in the string D:\a\n\t r'D:\a\n\t' # 2. with can open multiple files at once. with open() as f1,open() as f2,open() as f3:
II. Document reading and writing modes
1、'r' read-only mode read
read-only (computing),unwritten 1.File path does not exist:Direct error. with open(r'','r',encoding='utf8') as f: print(()) 2.File path exists:Normal readout with open(r'', 'r', encoding='utf8') as f: print(())
2、'w' write-only mode write
can only write,unwatchable 1.File path does not exist: # Create new documents with open(r'','r',encoding='utf8') as f: pass 2.File path exists: # Empty the contents of the file before rewriting it with open(r'', 'r', encoding='utf8') as f: ('Writing content') prerequisites: # 1, the contents of the preparation need to add their own line breaks, after saving u while to pay attention to the existence of his
3. 'a' trailing pattern add
Data can only be added at the end of the file 1.File path does not exist: # Create new documents with open(r'','a',encoding='utf8') as f: pass 2.File path exists: # Add at the end of the document with open(r'', 'a', encoding='utf8') as f: ('Writing content')
When we're writing code, there are some parts that we don't know the exact code for, but we can't just leave them blank.
This time you can use the keyword: pass
Role: to ensure the structural integrity of the program, otherwise it will report errors
III. Documentation operating modalities
1. t Text mode
Text mode is the default mode 1、Read/Write Mode Abbreviation:r、w、a full name:rt、wt、at prerequisites: # 1. Only text files can be operated # 2, read and write in characters # 3, you need to specify the encoding parameter, do not fill in the default encoding of the computer is used
2, b Binary mode
byteparadigm 1、不是默认的paradigm,It needs to be specified to generate rb、wb、ab prerequisites: # 1, can operate any file # 2. Reads and writes are in bytes. # 3. No need to specify the encoding parameter, no need for encoding
Regardless of the mode, it is the same for whether the file path exists or not
IV. Methods of documentation
1、read()
code performance: filename.read() prerequisites: # 1, read the contents of the file at once, and the cursor stays at the end of the file, continue to read no content # 2, when the content of the file is relatively large, the method may also cause the computer memory overflow # 3, the brackets can be filled with numbers, in the text mode, that reads a few characters # 4. Usually three bytes (or more) for a Chinese character and one byte for an English letter.
2、for loop
code performance: for data value in Documents to be traversed: prerequisites: # 1, line by line to read the contents of the file to avoid memory overflows
3、line
1、readline code performance: filename.readline() prerequisites: # 1. Read one line at a time 2、readlines code performance: filename.readline() prerequisites: # 1, read the contents of the file at once, will be organized in accordance with the number of lines into a list of individual data values
4、readable
code performance: code performance: filename.readable() prerequisites: # 1, to determine whether the file has the ability to read data
5、write
1、write Used to write specified data to a file code representation: f = open("", 'w') ("Write a new line of data.") () 2、writeable Used to determine if a file is writable code representation: f = open("", "a") print(()) can be written to: Ture unwritable: False 3、writelines Used to receive a list,Write all the data in the list at once code representation: f = open('', 'r') n = open('','w+') (()) () ()
6、flush
Used to flush the internal buffer, which is a best practice when dealing with fila processing in python to clear the internal buffer before writing/adding new text to the file. Flushing in-memory file data to hard disk immediately Equivalent to ctrl + s
Code denotes:
file_object.flush()
V. Movement of the cursor within the document
1、seek()
Move the read pointer of a file to a specified location。seekFunctions need to be called using a file object,No return value。 seek()There are three uses: seek(offset,whence) offset Controls the amount of displacement for cursor movement(nibbles) whence paradigm 0 基于文件开头移动多少nibbles 1 基于光标当前所在位置移动多少nibbles 2 基于文件末尾移动多少nibbles ps:1respond in singing2只能在二进制paradigm下使用 0cannot be said be
2、tell()
Used to get the current position of the cursor(Number of bytes moved) code representation: filename.tell() # Returns the number of bytes the cursor is currently in
The above is Python basic file operations and cursor movement details, more information about Python file operations cursor movement please pay attention to my other related articles!