This is the third day of my participation in the August More text Challenge. For details, see:August is more challenging

Part ONE: Basic operation of the file

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

Mode: Mode: Description:

R Opens a file in read-only mode. By default, the file pointer is placed at the beginning of the file. 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. If the file does not exist, an error is reported.

W Opens a file for writing, overwrites it if it already exists, or creates a new file if it does not.

W + Opens a file for reading and writing, overwrites the file if it already exists, or creates a new file if it doesn’t.

A opens a file for appending. 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 for writing.

A + opens a file for reading and writing. If the file already exists, the file pointer is placed at the end of the file. If the file does not exist, a new file is created for reading and writing.

Other modes: rb rb+ WB WB + ab AB + add b , perform the preceding operations.

1. How to open/close a file

Open file:

F = open(flie_path,mode = "r") # takes two arguments. The first argument is the location of the file (either absolute or relative), and the second argument is how to open it. By default, r is passed in a string representing the file path and returns a file object.Copy the code

Close file:

    f.close()
Copy the code

Call the close method on the given file object

Note: After opening a file, be sure to close it.

Tips to know:

(1) In Windows: Relative path: absolute path without drive letter: absolute path with drive letter

(2) Cursor position: 1. If you can’t read after reading, because the cursor position in the last 2. 3. After writing, the cursor position is shown in the last example:

>>f = open(r'C:\my\pycharm_work\ceshi.py'.'r+')
>>f.read()
'wumou love zhangmou'>>f.read() # can't read now because the cursor moved to the end after the last read.' '
>> f.seek(0) # Set the cursor position to the front, so there is content behind the cursor0>> f.read() #'wumou love zhangmou'>> f.tell() #24
>> f.seek(0)
0
>> f.tell()
0
Copy the code

Development:

  1. Read () # Specifies the length of the string to be read in parentheses

  2. Readline () # You can add an argument n in parentheses. If n is larger than the length of the string, you can only read the whole line, otherwise n characters are read

  3. Read multiple lines in a file: variable name. readlines() # specifies the argument n in parentheses.

  4. Write: f.write(variable name)

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

Here’s an example:

>> f = open(r'C:\my\pycharm_work\ceshi.py'.'r+')
>> f.read()
' '
>> f.writelines('wumou \n zhangmou \n haha')
>> f.seek(0)
0
>> f.read()
'wumouzhangmouzhangmouwumou \n zhangmou \n haha'
>> f.seek(0)
0>> f.readline() #'wumouzhangmouzhangmouwumou \n'
>> f.readline()
' zhangmou \n'
>> f.seek(0)
0>> f.readlines() # If n is added, only lines with more than n characters will be read, such as #n=5, the number of characters in the first line3The number of characters in the second line3, then these two lines will be read, because the number of characters in these two lines combined is greater than n. ['wumouzhangmouzhangmouwumou \n'.' zhangmou \n'.' haha']

Copy the code
  1. Flush the buffer immediately: variable name. flush() # can be interpreted as save

For example, the file content is 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') >> f2.read() # now you can't read the content you just appended to shuai'wumou'>> f1.flush() # We flush the buffer immediately, so you can read it below. >> f2.seek()0)
0
>> f2.read()
'wumoushuai'
Copy the code
  1. Get cursor position: variable name. tell()

  2. Adjust the cursor position: variable name. seek(position)

Summary: Persistent storage: Data stored in memory is easy to lose, only saved on the hard disk can be persistent storage, the basic method of saving on the hard disk is to write data to a file to open and close: in Python file opening and closing is very easy and fast, the file is automatically saved when closed