Common file Operations1. Open File
- Open, which is a built-in function that can be called directly
- Syntax: File object = open (file_name, [Access_mode]), where we create a file
- Parameters: file_name--The string value of the file name to access, access_mode--determines the mode of opening the file: read-only, write, append, and so on. This parameter is non-mandatory and the default file access mode is read-only (R)
- Return value: Returns a File object
Mode |
Explain |
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 |
r+ |
Open a file 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. Create a new file if it does not exist |
w+ |
Open a file for read-write. Overwrite the file if it already exists. Create a new file if it does not exist |
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 |
A + |
Open a file for writing. 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, a new file is created to read and write |
2. Close file: File_object.close ()3. File read and write operations
Read () Method:
- The Read (Size=-1) method reads a string from an open file
- parameters: size--Pass the number of bytes you want to read , if not passed, the default is-1, read all the data,
- When you're done reading. The pointer navigates to the location after the reading is completed.
- Return value: size=0, return B "", size<0, returns the number of bytes read, size>0, returns the specified number of bytes
ReadLine () Method:
- Reads an entire line from a file, including the "\ n" character
- Parameter: size--If a non-negative argument is passed, the number of bytes of the specified size is returned
- Return value: Returns the bytes read from the string
ReadLines () Method:
- Reads all rows and returns a list of strings, and returns an empty string if EOF is encountered
- Parameters: None
- Return value: Returns a list of all strings
Write () Method:
- The write () method writes any one string to an open file
- Note: The Write () method does not add a newline character ("\ n") at the end of the string, so wrapping is not possible. You need to manually add "\ n" to implement line breaks
Writelines () Method:
- By passing in the list you want to write, you can write multiple lines
4. Example
Suppose a new test.txt text file is created under the working directory of Pycharm, and because it contains Chinese, we save it with the code "Utf-8". The contents of the file are as follows:
Name:peter
Age:29
height:175
Weight:70
Country: United Kingdom
Hobby:play-guitar
Read the method
#open () and read () methodsfo= Open ("Test.txt") Content=Fo.read ()Print(content) Results: Nobelium 縩 ame:peterage:29Height:175Weight:70Country: Ã 卞浗 hobby:play-Guitar garbled, we can solve this: fo= Open ("Test.txt", encoding="Utf-8") Content=Fo.read ()Print(content) Results: name:peterage:29Height:175Weight:70Country: British Hobby:play-Guitar#ReadLines () methodFO = open ("Test.txt", encoding="Utf-8") Lines=Fo.readlines ()Print(lines) ['\ufeffname:peter\n','age:29\n','height:175\n','weight:70\n','Country: United Kingdom \ n','hobby:play-guitar\n','\ n']
How to write
Write () method: Overwrites the file if it already exists
# Write () method = open ("test.txt""w") fo.write (" City:london")
Fo.close () We open the Test.txt file and find that the file is left only: City:london
Note: In the same w mode, two writes, the first write will already exist, the second write will not overwrite the content of the first write (can be understood as the pointer moved)
fo = open ("test.txt""w", encoding="utf-8 " ) fo.write ("The first write overwrites the already existing content,") fo.write (" The second write does not overwrite the content that was written for the first time ")
Fo.close () Result: The first write overwrites content that already exists, and the second write does not overwrite the content that was written for the first time
Write () cannot implement line wrapping and requires "\ n" to be added manually
fo = open ("test.txt""w", encoding="utf-8 " ) fo.write ("The first write overwrites the already existing content, \ n") fo.write (" The second write does not overwrite the first written content ") fo.close () Result: The first write overwrites the already existing content, and the second write does not overwrite the content that was written for the first time
Writelines () passes a list to write multiple lines, does not wrap, and needs to be added manually
FO = open ("Test.txt","W", encoding="Utf-8") Fo.write ("The first write overwrites what already exists, \ n") Fo.write ("the second write does not overwrite the content that was written for the first time \ n") Fo.writelines (["111111\n","222222\n"]) Fo.close () Results The first write overwrites content that already exists, and the second write does not overwrite the content that was written for the first time111111222222
Additional Methods
fo = open ("test.txt""a", encoding="utf-8 " ) fo.write (" haha haha ") fo.close () Result: The first write overwrites the already existing content, The second write does not overwrite the content that was written for the first time 111111222222 haha haha
5. Document Positioning
the Tell () and Seek () methods are described in: "Translate": Methods of File Objects
Common file and directory operations in Python (i)