用户/应用程序可以通过文件将数据永久保存的硬盘中

如何用文件

open()函数源码

  1. ========= ===============================================================
  2. Character Meaning
  3. --------- ---------------------------------------------------------------
  4. 'r' open for reading (default)
  5. 'w' open for writing, truncating the file first
  6. 'x' create a new file and open it for writing
  7. 'a' open for writing, appending to the end of the file if it exists
  8. 'b' binary mode
  9. 't' text mode (default)
  10. '+' open a disk file for updating (reading and writing)
  11. 'U' universal newline mode (deprecated)
  12. ========= ===============================================================
  13. The default mode is 'rt' (open for reading text). For binary random
  14. access, the mode 'w+b' opens and truncates the file to 0 bytes, while
  15. 'r+b' opens the file without truncation. The 'x' mode implies 'w' and
  16. raises an `FileExistsError` if the file already exists.

解析

  1. 控制文件读写内容的模式:tb
  2. 强调:tb不能单独使用,必须跟r/w/a连用
  3. t文本(默认的模式)
  4. 1、读写都以strunicode)为单位的
  5. 2、文本文件
  6. 3、必须指定encoding='utf-8'
  7. b二进制/bytes
  8. 控制文件读写操作的模式
  9. r只读模式
  10. w只写模式
  11. a只追加写模式
  12. +:r+、w+、a+