I. Opening and closing of files
Open (file name, access mode)
Cloese ()
| Mode |
Description |
| 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. |
Ii. Reading and writing of documents
F.read ()
F.readline () reads a single line of readable large files
F.write ()
f = open ('1.txt','w') f.write ('Hello Sqyy') f.close ()
Third, document positioning
The tell () method tells you the current position within the file, in other words, the next read and write occurs after so many bytes at the beginning of the file.
The Seek (offset [, from]) method changes the position of the current file. The offset variable represents the number of bytes to move. The from variable specifies the reference position at which to begin moving bytes.
If from is set to 0, this means that the beginning of the file is used as the reference location for moving bytes. If set to 1, the current position is used as the reference location. If it is set to 2, then the end of the file will be used as the reference location.
#-*-coding:utf-8-*-f = open ('1.txt','r+')#Find Current LocationPosition =F.tell ()Print "Current file location:", Position#Reposition The pointer here at the beginning of the filePosition =F.seek (0,0); Str=F.read ()Print "re-read string", Strf.close ()
Iv. renaming and deletion of files
1. Renaming files
Rename () in the OS module can complete renaming of files
Rename (need to modify the file name, the new file name)
1 Import OS 2 os.rename ("1.txt""2.txt")
2. Deletion of files
Remove () in the OS module can complete the deletion of the file
Remove (file name to be deleted)
1 Import OS 2 os.move ("1.txt")
V. Related Operations of folders
In actual development, it is sometimes necessary to use the program to do certain things, such as creating, deleting, etc.
Just as the OS module is required for file operations, the OS module is also required if you want to manipulate folders
1. Create a folder
Import Osos.mkdir ("sqyy")
2. Get the current directory
Import OSOS.GETCWD ()
3. Change the default directory
Import Osos.chdir (".. /")
4. Get directory List
Import Osos.listdir ("./")
5. Delete Folder
Import Osos.rmdir ("sqyy")
"Code Learning" PYTHON file I/O