python file manipulation functions (update 1)

The file operation function is in the module os. When using, import the os module first.
1. open(name) function: open the file named name, the file name can have a path name. The name parameter is required, otherwise an error will occur. If there is Chinese in the content of the file to be opened, it should be opened in the following way:
open('1.py','r',encoding='utf-8') # r means to open the file in read-only mode, and the default way to open the file Also read-only, so r can be omitted.
Note:
1) If the second parameter of the open() function is: a, it means to open the file in append mode. At this time, if the name file does not exist, a new file named name will be created. Always write from the end of the file.
2) If the second parameter of the open() function is: w, it means to open the file in writing mode. At this time, if the name file does not exist, a new file named name will be created, but if the file already exists, will be cleared.
Note:
3) When opening a file in read-only mode (r parameter), the file can be opened multiple times.
4) When the file is opened in the mode of appending (a parameter) or writing mode (w parameter), the file must be closed with the close() function before it can be opened again. Files opened in the append mode cannot be read, but can only be written, and no matter where the file pointer is, the content will be appended from the end, and the file pointer will be at the end of the file after the append is completed. But in append mode, truncate() can be used normally.

2, read () function: without parameters is to read the entire file into a string. For text files, read(10) means to read 10 characters. For binary files, read(10) reads 10 bytes.
3. readlines() function: read the file into a list of strings, each string in the list is a paragraph (bounded by carriage return '\n').
Both read() and readlines() start reading from the current file pointer position.

4. write(string) function: write the string parameter into the file currently opened for appending or writing. The string parameter can have \n as a carriage return. The writing start position is the current pointer position of the file.
Note:
After opening a file for writing or appending, if you want to read the file, you must close it first, and then open it as read-only.
writable() function: Tests whether the file is writable.

5. close() function: close the currently open file.

6. Flush() function: force to refresh the updated content to the hard disk.

7. seek() function: move the pointer position in the file, such as seek(0) to move to the file head. The seek() function has parameters: offset, from_where. offset means to move the pointer to the third position; from_where has three parameters:
0: means to move from the beginning of the file,
1: means to move from the current position,
2: means to move from the end of the file.
**Note: **seek() offset is in bytes, no matter what kind of file it is.

8. tell() function: determine the position of the file pointer.

9. The truncate() function: truncate the file, such as: truncate(100), means to intercept 100 characters from the beginning of the file, and discard all the characters behind. When there is no parameter, truncate from the current pointer position of the file. After this function is executed, it is saved to disk immediately, and there is no need to execute flush(). truncate() can empty the file with the following code (assuming f is the open file):

f.seek(0)
f.truncate()

Guess you like

Origin blog.csdn.net/any1where/article/details/128175144
Recommended