python base-file processing

File Processing

Three big questions about files: What is a file?Why use files?How do I use files?

  • File (File System) is a tool provided by the operating system to operate hard disks
  • The file system is used to permanently save data

How to operate the file

  • Relative and absolute paths

    • Relative path: The path from the current path relative to a base directory
    • Absolute path: The path from the root directory, which is also the drive letter

    Relative paths are recommended in programming

  • Two ways to manipulate files

    • open

      Syntax: f = open('file name path', mode,encoding), used to open a file.

      File Path: Relative Path is recommended

      mode: There are three modes to open a file: r, w, a.The two modes of general and processing files apply.

      The two modes for processing files are t and b.

      Encoding: open the file, manipulate the encoding settings of the file contents

      # The first way to open, which requires f.close() to shut down system resources
      f = open("a.txt","r",encoding="utf-8")
      data = f.read()     # Read everything in a.txt file
      print(data)         # Print the contents of the file
      f.close()           # Turn off operating system resources
      Pattern describe Characteristic
      rt Default mode, which can be abbreviated as r.Open file in text file and read-only mode Error if file does not exist
      wt It can be abbreviated as w.Open file in text file and write-only mode If the file does not exist, create a new file; if there is data in the file, override the contents of the file
      at Abbreviated as a, opens the file in text file and write-only mode If the file does not exist, create a new file; if there is data in the file, append the content after the data.
      rb Open file in binary character and read-only mode Error if file does not exist
      wb Open file in binary character and write-only mode If the file does not exist, create a new file; if there is data in the file, override the contents of the file
      ab Open file in binary character and write-only mode If the file does not exist, create a new file; if there is data in the file, append the content after the data.
    • with open() as ...:

      Syntax: with open (File Name Path, mode,encoding)as f: This is the recommended way to manipulate files and open them contextually.Advantages: Not only does it automatically recycle system resources, but it can also open the same file or multiple files in multiple modes at the same time.

      # The second way to open, context-sensitive, automatically shuts down system resources is recommended
      with open("a.txt","r",encoding="utf-8") as f:
          data = f.read()
          print(data)
      
      # Open multiple files at once, separated by commas
      with open("a.txt","r",encoding="utf-8")as rf,\
          open("b.txt","w",encoding="utf-8") as wf:
          data = f.read()
          wf.write(data)
  • Built-in methods for file processing

    # Contents in a.txt file
     Name: sean
     Age: 18
     Gender: Male
     Description: The most handsome people in the Shanghai school district
    • read

      grammar Explain
      f.read() By default, all file contents are read out at once; parameters are based on the mode of the model, if t, the number of characters to read the file; if b, the number of bytes to read the file
      f.readline() Only one line can be read at a time
      f.readlines() Read all the data into memory and store it in a list separated by line breaks
      f.readable() Determines whether the file is readable and returns a Boolean value
      # Mode is: read-only text mode, r=rt is read text
      with open("a.txt","r",encoding="utf-8") as f:
          print(f.readable())     # The printed result is:True
          print(f.readline())     # The printout is: Name: sean
          print(f.readlines())    # The results are as follows: List ['Age: 18\n','Gender: Mann','Description: Most Narcissistic Person in Shanghai School District']
          f.seek(0,0)             # Move the cursor to the beginning of the file
          print(f.read())         # The printout is: All content
      # Mode is: read-only byte mode, rb is read bytes.Note: Using byte mode, the encoding parameter is not available
      with open("c.txt","rb") as f:
          print(f"readable Results:{f.readable()}")     # Print result: True
          print(f"read Results:{f.read()}")         # Print result: binary bytes of all contents of the file
          f.seek(0,0)             # Move the cursor to the beginning of the file
          print(f"readline Results:{f.readline()}")     # Print result: Binary bytes recorded on the first line of file contents
          f.seek(0,0)
          print(f"readline Results:{f.readlines()}")    # Print result: list of binary bytes

      Print results:

    • write

      grammar Explain
      f.write() Write data to file
      f.writelines Writes elements of an iterative object to a file as strings.Equivalent to for+write()
      f.writeable Determines whether a file is writable and returns a Boolean value
      # Write-only text mode, empty the original content of the file, rewrite the new content
      with open(r"a.txt","w",encoding="utf-8") as wf:
          print(f"Readable:{wf.readable()}")      
          print(f"Writable:{wf.writable()}")
          wf.write("This is used write New Written Content\n")
          wf.writelines(["This is used writelines New Write 1\n","This is used writelines New Write 2\n","This is used writelines New Write 3\n"])      # The list is stitched together and written to the file
      
      # Verify that content is written to a.txt file
      with open(r"a.txt","r",encoding="utf-8") as  rf:
          print(rf.read())

      Operation Result

      # Append text mode, preserve the original contents of the file, and append new data after
      with open(r"a.txt","a",encoding="utf-8") as af:
          print(f"Readable:{af.readable()}")
          print(f"Writable:{af.writable()}")
          af.write("This is using the append mode write Written Content\n")
          af.writelines(["This is using the append mode writelines New Write 1\n","This is using the append mode writelines New Write 2\n","This is using the append mode writelines New Write 3\n"])
      # Verify that content is written to a.txt file
      with open(r"a.txt","r",encoding="utf-8") as  rf:
          print(rf.read())

      Operation Result

    • Operation of cursor (pointer)

      grammar describe
      f.seek(offset,whence) offsetf: relative offset, the number of bits the cursor moves, for bytes;
      whence: Used to specify where the cursor position begins.
      Values can only be: 0 (at the beginning of the file), 1 (at the current location), and 2 (at the end of the file).
      f.tell() Location of cursor
  • Be careful:
    • In utf-8, Chinese characters account for 3 bytes and English characters for 1 bytes
    • In gbk, Chinese characters account for 2 bytes and English characters for 1 bytes
    • When working with files, it is important to pay attention to the coding rules, and remember the core of keeping the code clean: what code to use to store the data, what code to use to get it

#####Extension

We learn to read files r, w, a in pure mode, as well as r+, w+, a+ mode.These three modes are set to read-write mode, and the other features are the same as pure mode.

Keywords: Python encoding Programming

Added by numerical25 on Sun, 10 Nov 2019 08:45:01 +0200