“This is the third day of my participation in the August More Text Challenge. For details, see: August More Text Challenge.”

1. Basic operation of files

  • File open format:

    • File = open (file path, read/write mode)

      • File path: You can write a relative path or an absolute path
      • Read/write mode: R (read) W (write) A (append)
  • Once the file is open, it must be closed or it will continue to consume server performance.

1. Open file # 2. Open the file by using the open function. File_name (file_name), mode(read/write mode) using this function returns a file object # file path: can write relative path, can write absolute path, path needs to be passed in string # read/write mode: R (read only) w(write) a() append file = open('python. TXT ', 'r') print(file) # < _io.textioWrapper name='python.txt' mode='r' encoding=' utF-8 '> # <class '_io.textioWrapper '> # print(file.read()) When the file is closed, the file object is released. When the file is closed, the file object is released. Print (file) # <_io.TextIOWrapper name='python.txt' mode='r' encoding=' utF-8 '> # Print (file) # <_io ValueError: I/O operation on closed file # ValueError: I/O operation on closed file Cannot operate on a closed file print(file.read())Copy the code

2. Read files

  • Read: If () is filled with a number, the string of the specified character is read. Each time the specified character is read, after a file is opened, multiple reads will continue to read the character backwards. If all characters are read, the empty string “” will be returned.

    • Format: file object read(maximum number of characters read at a time)
  • If the file to be read does not exist, an error is reported

File = open('python. TXT ', 'python. TXT ', 'python. 'r') # read file # n: Pass a value in the read, representing the maximum number of characters we can read # If we have a text file in development, such as a web novel,4 G size, read at one time, the user reads such a large file in turn, which is extremely performance consuming, and the wait time is too long # The maximum number of characters that can be read is (1024*1024) #. The maximum number of characters that can be read is (1024*1024) #. When the file is read, it will continue to read backwards until the file is closed or the program is finished, so you can use a loop to read the file. Content = file.read(3) if content == ": break print(content)Copy the code
  • Readline: read data one line at a time, delimited by \n. After a file is opened, repeated reads will continue to read backwards. If all characters are read, the empty string “” is returned.

    • Format: file object.readline ()
  • Readlines: Reads the entire file at once. After reading, returns the text as one element in a list

    • File object.readlines ()
File = open('python. TXT ', 'r') # file operation # readline reads a file in this way, one line at a time delimited by \n, and while the file is open, it continues to read down until all files have been read, then an empty string is read "" # while True: # content = file.readline() # if content == '': # break # print(content,end= ") # break # print(content,end= ") # break # print(content,end= ") # readlines \n'] content = file.readlines() print(content) #Copy the code

3. Write operations to files

  • Open the file with the write mode ‘w’

    • If the file exists, the source data is cleared
    • If the file does not exist, a new file is created and no error is reported
  • Use write to write characters

  • When writing and reading files on Windows computers, use encoding to specify the encoding format

    • Format: open (file path, read-write mode, encoding = encoding)
# file = open('test.txt') # file = open('test.txt') 'w') # When a file is opened in write mode, if the opened file exists, it will empty the characters in the source file. # If you are developing on a Windows computer, you will need to write to the file in a 'UTF-8' encoding UnicodeDecodeError: File = open('python.txt', 'w', encoding=' utF-8 ') 'gbk' codec can't decode byte 0x89 in position 14: Illegal multibyte sequence print(file) # < _io.textioWrapper name='python. TXT 'mode='w' encoding='UTF-8'> # Write operation # File. Write (' I love Tian 'anmen, the sun rises on Tian 'anmen ') Write (""" I love Tiananmen Square in Beijing, # file. Writelines (' I love Beijing Tian 'anmen ') lines = [' Wu Si, Su Tong, Zhang Gaoqiu \n', "Empty mountains condensation clouds never flow \n", "look up at the moon \n", "bow down and think of home \n"]Copy the code

4. Append files

  • ‘a’ : Files are opened in mode

    • If the file does not exist, a new file is created
    • If the file exists, the string is appended to the original file without emptying the source file
  • In the append mode, write is used to write files. There is no separate append method, and the writing mode is the same as that in the ‘W’ mode

# file = open('python. TXT ', 'a') # file = open('python. TXT ', 'a') # file = open('python. TXT ', 'a') File = open('bigdata.txt', 'a', encoding=' utF-8 ') Encoding =' utF-8 ') # Encoding =' utF-8 ') # Encoding =' utF-8 ') # Encoding =' UTF-8 ') # Encoding =' UTF-8 'Copy the code

\