Operation on the file:
1, first open the file, open the file need to specify open mode (open mode contains: R, W, A, r+, w+, A +, RB, WB, AB, rb+, wb+, ab+), character set;
2, write to the file, read the file, print the cursor position, change the position of the cursor;
3, the file refresh, write to disk;
Mode Description:
| 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. |
|
File Operation Example:
#author FK
Import Sys,time #使用w模式, overwrite the original file and write from the beginning F=open ("Test.txt", ' W ', encoding= "Utf-8") F.write ("I am a little bird!!!" \ n ") F.write ("I want to fly higher and higher. \ n ") F.close ()
#使用r模式, can only read and cannot be modified F1=open ("Test.txt", "R", encoding= "Utf-8") Print (F1.read ()) F1.close ()
#使用a +, may append and be readable, note append at end of file F2=open ("Test.txt", "A +", encoding= "Utf-8") #写读, create the file first, then write the read again #f2 =open ("Test.txt", "w+", encoding= "Utf-8"). #读写, open the file first, read and write it first #f2 =open ("Test.txt", "r+", encoding= "Utf-8") #以二进制格式读文件 #f2 =open ("Test.txt", "RB")
F2.write ("This is the appending text1\n") F2.write ("This is the appending text2\n")
#打印操作系统中打印文件的个数 Print ("Print open files Number:", F2.fileno ())
#打印文件中的光标的位置 Print ("Before Seek method, print the F2.tell method:", F2.tell ()) #将光标移动到0位置, read from the beginning. F2.seek (0) Print ("After Seek method, print the F2.tell method:", F2.tell ())
#逐行读文件 Line_Count = 0 For F22 in F2: If Line_Count = = 0: Line_Count +=1 Continue Print (f22,end= ") Line_Count +=1
#截断, truncated from the specified position, starting from the beginning of the calculation, regardless of where the cursor is located F2.seek (300)
F2.truncate (30) #打印进度条 Print ("\ n Printing progress bar test:") For I in range (5): Sys.stdout.write ("#") Time.sleep (1) #flush Sys.stdout.flush ()
#关闭文件 F2.close () |
Python Operations on files