Python File Operations Readiness: 1 It is very simple to use Python to read a file, and we use the open () function for opening a file, obtaining a handle to the file, and then using the file to do all sorts of things. There are also differences in the actions you can perform depending on how you open them. 2) How to open the file: R,w,a,r+,w+,a+,rb,wb,ab,r+b,w+b,a+b is using the R (read-only) mode by default. 3) Relative path: Compared to the folder where the current program is located ... /returns the absolute path to the previous directory: 1. Look for it from the root directory of the disk. 2. An absolute path on the Internet. 4) F = open (' File path ', mode= ' File open mode ', encoding= ' encoded format ') #mode = can be omitted. Passf.close () The above three sentences can be changed to with open (' File path ', mode= ' File open mode ', encoding= ' encoded format ') as F:passwith This will be done by itself after closing the handle. 5) According to the form of the data in the file, divide the file into two types: text file and binary file. 6) Handle: Popular is the operation of the file baton, if you open a file is not closed, but open other files also use the handle, this time the file does not know who to listen to. Common methods for file operations: 1) Flush () writes the contents of the buffer to disk without closing the file. 2) Close () writes the contents of the buffer to disk, closes the file, and releases the file object. 3) Read (size) reads the contents of a size from the file as a result, and if size is omitted, the contents of the entire file are read as the result of the return. 4) ReadLine () reads 1 rows from a text file as a string return. 5) ReadLines () inserts each line in the text file as a string into the list, returning the list. 6) Seek (offset,whence) move the file pointer to a new location. Offset represents the position relative to the whence. Whence is used to set the starting point for the relative position: 0 is calculated from the beginning of the file, 1 is calculated from the current position, and 2 means the calculation starts at the end of the file. If whence is omitted, offset indicates the position relative to the beginning of the file. 7) Tell () returns the position of the current file pointer 8) write (s) writes the contents of the string s to a text file or to a binary file. 9) Writelines (List[anystr]) writes a list of strings to a text file without adding a line break. Truncate (size) removes the content from the current pointer position to the end of the file. If size is specified, the rest is deleted regardless of where the pointer leaves a size byte. File operation:1) r opens a text file in read-only mode and runs only the read data, and an exception occurs if the open file does not exist. Example: <1>f = open ("file.txt", mode= "R", encoding= "Utf-8") #创建句柄s = F.read () f.close () # Close handle print (s) <2>f = open (" File.txt ", mode=" R ", encoding=" Utf-8 ") for lines in F: # reads one line at a time. Assign a value to the front line variable print (line) #print (F.readline ()) #读一行f. Close () <3>with open ("file.txt", mode= "R", encoding= "Utf-8" As F:print (F.readlines ()) #将每行内容存入列表中2) r+ Open a text file in read-write mode, do not delete the original content, allow read and write, if the open file does not exist, it will produce an exception. Example: <1>f = open ("file.txt", mode= "r+", encoding= "Utf-8") # r+ mode, by default the cursor at the beginning of the file, must read first, then write F.write ("Week Star") # This changes the first three data in the top row of the file to the weekly star, and the cursor stays behind the week star S = F.read () #这里读出的内容为: All content after the cursor F.flush () f.close () print (s) <2>with open ("Boutique", Mode= "r+", encoding= "Utf-8") as F:s = F.read (3) # No matter how many you read before. The back to write is at the end of the F.write ("haha") # 1. Write before nothing is done. Write #2 at the beginning. If some content was read. Write again, write last print (s) 3) W open a text file in write-only mode, delete the original content, and allow only write data. If the open file does not exist, it is created and opened. Example: F = open ("file.txt", mode= "W", encoding= "Utf-8") # Write before the original content F.write ("ten") # F.writelines ([' 1 ', ' 2 ', ' 3 ']) #不添加换行符f. f Lush () F.close () 4w+ open a text file as read/write, delete the original content, allow read and write, and open the file if it does not exist. Example: <1>f = open ("file.txt", mode= "w+", encoding= "Utf-8") # W operation. The original content will be emptied. Print (F.read ()) #读出一个空行 because it was emptied f.write (" What's the weather like Today ") F.seek (3) #移动3个字节, that is, a man s = F.read () #读出的内容为 ... Print (s) #天天气怎么样f. Flush () f.close () <2>f = open ("Profane", mode= "w+", encoding= "Utf-8") # W operation. The original content will be emptied. F.write (" What's the weather like Today ") #此时光标在尾部f. Seek (3) #默认从0开始的f. Write (' Hello ') #这时指针在好之后气之前 (the file contains: How are you today) s = F.read () print (s) #气怎么样f. Seek (0) s = F.read () print (s) #今你好气怎么样f. Flush () F.close () 5) a appends to open a text file, does not delete the original content, allows to write data at the end of the file, if the open file does not exist, then new and open. Example: With open ("file.txt", mode= "a", encoding= "Utf-8") as f:# on the original basis for appending content. (no matter where the cursor is) f.write ("Beauty") F.flush () 6) A + reads and writes a text file, does not delete the original content, allows to read at any location, but can only append data at the end of the file, if the open file does not exist, then new and open. Example: F = open ("file.txt", mode= "A +", encoding= "Utf-8") # Add content on the original basis. #文件内容: Hello F.seek (3) s = F.read () #读取内容print (s) #好f. Write ("Little Dragon Girl") #添加到队尾f. Flush () F.close () 7) rb,rb+,wb,wb+,ab,ab+ They open a binary file, and the other operation is the same as the text file without B. Example: With open (".. /eaten ", mode=" RB ") as F:content = F.read () the print (content) result is: B '\xe7\x83 .... '
Python file operations