Open File:
File_obj =file ("File path", "mode")
There are several modes of opening a file:
R, open the file as read-only
W, open a file for writing only. If the file already exists, it will overwrite it, overwriting the previous content with the new content, and if the file does not exist, a new file will be created
A, open a file for append. If the file already exists, the file pointer will be placed at the end of the file. In other words, the new content will be written to the existing content. If the file does not exist, create a new file to write to.
w+, open a file for read-write. Overwrite the file if it already exists. If the file does not exist, create a new file.
The inductive summary model is as follows:
| Mode |
Describe |
| R |
Open the file as read-only. The pointer to the file will be placed at the beginning of the file. This is the default mode. |
| Rb |
Opens a file in binary format for read-only. The file pointer will be placed at the beginning of the file. This is the default mode. |
| r+ |
Open a file for read-write. The file pointer will be placed at the beginning of the file. |
| rb+ |
Opens a file in binary format for read-write. The file pointer will be placed at the beginning of the file. |
| W |
Open a file for writing only. Overwrite the file if it already exists. If the file does not exist, create a new file. |
| Wb |
Open a file in binary format only for writing. Overwrite the file if it already exists. If the file does not exist, create a new file. |
| w+ |
Open a file for read-write. Overwrite the file if it already exists. If the file does not exist, create a new file. |
| wb+ |
Opens a file in binary format for read-write. Overwrite the file if it already exists. If the file does not exist, create a new file. |
| A |
Opens a file for appending. If the file already exists, the file pointer will be placed at the end of the file. In other words, the new content will be written to the existing content. If the file does not exist, create a new file to write to. |
| Ab |
Opens a file in binary format for appending. If the file already exists, the file pointer will be placed at the end of the file. In other words, the new content will be written to the existing content. If the file does not exist, create a new file to write to. |
| A + |
Open a file for read-write. If the file already exists, the file pointer will be placed at the end of the file. The file opens with an append mode. If the file does not exist, create a new file to read and write. |
| ab+ |
Opens a file in binary format for appending. If the file already exists, the file pointer will be placed at the end of the file. If the file does not exist, create a new file to read and write. |
Obj.read, Obj.readlines, Obj.xreadlines () Differences:
Read the contents of the file:
#一次性加载所有的内容到内存
Obj.read ()
#一次性加载所有的内容到 and split into strings based on rows
Obj.readlines ()
#每次仅仅读取一行数据
For line in obj:
Print Line
#每次循环, read only one line to avoid all memory read-in
Write the contents of the file:
Obj.write (' content ') #----> Write the data in memory into the file
Obj.writeline (' content ') #----> Write data to a file in a row
To close a file handle:
Obj.close ()
Example code:
See annex:
Homework:
Job One: Blog
Record the content of your studies on your blog
Job Two: Writing the login interface
Requirements:
Enter User name password
Authentication successful Display Welcome message
Three-time error after locking
Job Three: Multilevel menu
Level three Menu
You can select to go to each submenu in turn
Required Knowledge: list, dictionary
This article from "Flat Light is true" blog, please be sure to keep this source http://cryan.blog.51cto.com/10837891/1710849
Basic operation of the file