“This is the fifth day of my participation in the Gwen Challenge in November. Check out the details: The Last Gwen Challenge in 2021.”

The first part: : : basic operation of the file

R read (read); Write w (write); Perform x; Append a (append)

File opening mode: Mode: Description: r Opens the file in read-only mode. The file pointer is placed at the beginning of the file by default. If the file does not exist, an error is reported. R + Opens a file for reading and writing. The file pointer is placed at the beginning of the file by default. An error is reported if the file does not exist. W opens a file for writing, overwrites it if it already exists, and creates a new file if it doesn’t. W + opens a file for reading and writing, overwrites it if it already exists, and creates a new file if it doesn’t. A opens a file for appending, and if the file already exists, the file pointer is placed at the end of the file (that is, the new content will be written after the existing content). If not, a new file is created to write to. A + Opens a file for reading and writing. If the file already exists, the file pointer will be placed at the end of the file. The file will be opened in append mode.

Other modes: rb rb+ wb wb+ ab ab+ plus ab To perform the preceding operations.

1. How do I open/close a file

Open file:

F = open(flie_path,mode = "r") # take two arguments, the first argument is the location of the file (can be absolute path, can be relative path), the second argument is the open method, by default r passes a string representing the file path, will return a file object.Copy the code

Close the file:

f.close()
Copy the code

Calls the close method of the given file object

Note: After opening a file, always close it.

(1) In Windows: Relative path: Absolute path without a drive letter (2) Cursor position: 1. 2. Use the seek method to reposition the cursor 3. After writing, the cursor position is in the last example to look at:

>>f = open(r'C:\my\pycharm_work\ceshi.py','r+') >>f.read() 'wumou love zhangmou' >>f.read() #  ">> f.seek(0) 0 >> f.read() # 'wumou love zhangmou' >> f.tell() # 24 >> f.seek(0) 0 >> f.tell() 0Copy the code

Read () # Specify the length of the string to be read in parentheses

Readline () # if n is larger than the length of the line, only the entire line can be read

Readlines () # you can specify n in parentheses. If you do not write, you will read all. If you read all, you will change to list.

4. Write: f.write(variable name)

5. Write multiple lines :(note that newlines need to add their own code)

Here’s an example:

>> f = open(r'C:\my\pycharm_work\ceshi.py','r+') >> f.read() '' >> f.writelines('wumou \n zhangmou \n haha') >> F.s eek (0) 0 > > f.r ead () 'zhangmou wumouzhangmouzhangmouwumou \ n \ n haha > > f.s eek (0) 0 > > f.r eadline # () this is the check. 'wumouzhangmouzhangmouwumou \ n' > > f.r eadline () 'zhangmou \ n > > f.s eek (0) 0 > > f.r eadlines # () this is the check all lines. If n is added, then only lines greater than n will be read. For example, #n=5, the first line has 3 characters and the second line has 3 characters, then these two lines will be read, because the two lines together have # greater than n. ['wumouzhangmouzhangmouwumou \n', ' zhangmou \n', ' haha']Copy the code

6. Flush the buffer immediately: variable name

For example \ : the file contents are: wumou

>> f1 = open(r'C:\my\pycharm_work\ceshi.py','a') >> f1.write('shuai') 5 >> f2 = open(r'C:\my\pycharm_work\ceshi.py','r') Shuai 'wumou' >> f1.flush() # We flush the buffer immediately, so we can read it below. >> f2.seek(0) 0 >> f2.read() 'wumoushuai'Copy the code

7. Get cursor position: variable name.tell ()

8. Adjust cursor position: variable name seek(position)

Summary: Persistent storage: Data stored in memory is easily lost, and can only be persisted if stored on hard disk. The basic method of storing data on hard disk is to write data to a file to open and close it. Opening and closing files in Python is very easy and fast, and files are automatically saved when closed