<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
  <channel>
    <title>IO on 老张开工了</title>
    <link>/io/</link>
    <description>Recent content in IO on 老张开工了</description>
    <generator>Hugo</generator>
    <language>zh-cn</language>
    <lastBuildDate>Wed, 13 May 2026 00:00:00 +0000</lastBuildDate>
    <atom:link href="/io/index.xml" rel="self" type="application/rss+xml" />
    <item>
      <title>文件读写与 IO 操作</title>
      <link>/posts/2026/05/file-io/</link>
      <pubDate>Wed, 13 May 2026 00:00:00 +0000</pubDate>
      <guid>/posts/2026/05/file-io/</guid>
      <description>文件操作是编程中最常见的任务之一——读取配置文件、写入日志、处理数据文件、导入导出……几乎每个程序都需要和文件打交道。&#xA;Python 的文件操作接口设计得既简洁又强大。本章将从基础的 open() 函数开始，逐步深入到上下文管理器、CSV 处理、JSON 读写、现代路径操作以及临时文件的处理。&#xA;open() 函数 open() 是 Python 文件操作的入口，返回一个文件对象（file object）：&#xA;# 打开文件读取 file = open(&amp;#34;example.txt&amp;#34;, &amp;#34;r&amp;#34;, encoding=&amp;#34;utf-8&amp;#34;) content = file.read() file.close() # 务必关闭！ print(content) 文件模式 open() 的第二个参数是模式（mode），决定了你要对文件做什么操作：&#xA;模式 含义 文件指针位置 &amp;quot;r&amp;quot; 读取（默认） 开头 &amp;quot;w&amp;quot; 写入（覆盖已有内容） 开头（清空文件） &amp;quot;a&amp;quot; 追加 末尾 &amp;quot;x&amp;quot; 排他创建（文件已存在则报错） 开头 &amp;quot;r+&amp;quot; 读写 开头 &amp;quot;w+&amp;quot; 读写（覆盖） 开头（清空文件） &amp;quot;a+&amp;quot; 读写（追加） 末尾 加上类型修饰符：&#xA;修饰符 含义 &amp;quot;b&amp;quot; 二进制模式（如 &amp;quot;rb&amp;quot;、&amp;quot;wb&amp;quot;） &amp;quot;t&amp;quot; 文本模式（默认，如 &amp;quot;rt&amp;quot; 等价于 &amp;quot;r&amp;quot;） # 各种模式的示例 # 读取文本文件 with open(&amp;#34;data.</description>
    </item>
  </channel>
</rss>
