文件操作是编程中的一个重要部分,通过文件操作,我们可以将数据持久化存储在磁盘上,便于后续的读取与处理。本章将介绍如何在 Python 中进行文件的读写与操作。

1. 文件读写

1.1 打开与关闭文件

在 Python 中,使用 open() 函数可以打开文件,使用 close() 函数可以关闭文件。open() 函数返回一个文件对象,支持两种模式:

  • 读取模式:r
  • 写入模式:w
  1. # 打开文件进行读取
  2. file = open('example.txt', 'r')
  3. # 读取文件内容
  4. content = file.read()
  5. print(content)
  6. # 关闭文件
  7. file.close()

为了避免忘记关闭文件,可以使用 with 语句,它会在代码块执行完毕后自动关闭文件:

  1. with open('example.txt', 'r') as file:
  2. content = file.read()
  3. print(content)

1.2 读取文件内容

可以通过多种方式读取文件内容:

  • read(size):读取指定大小的字符
  • readline():读取单行内容
  • readlines():读取所有行并返回一个列表
  1. with open('example.txt', 'r') as file:
  2. # 读取完整内容
  3. content = file.read()
  4. print(content)
  5. # 读取单行内容
  6. file.seek(0) # 重置文件指针
  7. line = file.readline()
  8. print(line)
  9. # 读取所有行
  10. file.seek(0) # 重置文件指针
  11. lines = file.readlines()
  12. print(lines)

1.3 写入文件内容

写入文件时,如果文件不存在将自动创建;如果文件存在则会覆盖原有内容。使用 write() 方法可以写入字符串内容。

  1. with open('example.txt', 'w') as file:
  2. file.write("Hello, Python!\n")
  3. file.write("This is a new line.")

可以使用追加模式 a 来在文件末尾追加内容:

  1. with open('example.txt', 'a') as file:
  2. file.write("\nAppending a new line.")

2. 文件与目录操作

除了读写文件内容,Python 还提供了一些模块用于文件与目录的操作。

2.1 文件与目录的创建、删除与重命名

使用 os 模块可以进行文件和目录的操作。

  1. import os
  2. # 创建文件
  3. with open('new_file.txt', 'w') as file:
  4. file.write("This is a new file.")
  5. # 创建目录
  6. os.mkdir('new_directory')
  7. # 删除文件
  8. os.remove('new_file.txt')
  9. # 删除目录
  10. os.rmdir('new_directory')
  11. # 重命名文件
  12. os.rename('example.txt', 'renamed_example.txt')

2.2 路径操作与文件检查

os.path 模块提供了对路径和文件检查的支持。

  1. import os
  2. # 获取文件绝对路径
  3. abs_path = os.path.abspath('renamed_example.txt')
  4. print(abs_path)
  5. # 检查文件是否存在
  6. exists = os.path.exists('renamed_example.txt')
  7. print(exists)
  8. # 检查是否为文件
  9. is_file = os.path.isfile('renamed_example.txt')
  10. print(is_file)
  11. # 检查是否为目录
  12. is_dir = os.path.isdir('some_directory')
  13. print(is_dir)

2.3 示例:读取配置文件

读取配置文件是文件操作的一个典型应用场景。以下是一个读取配置文件的示例:

假设我们有一个配置文件 config.txt,内容如下:

  1. [settings]
  2. username = admin
  3. password = 12345

我们可以使用以下代码读取并解析配置文件:

  1. def read_config(file_path):
  2. config = {}
  3. with open(file_path, 'r') as file:
  4. for line in file:
  5. if '=' in line:
  6. key, value = line.strip().split('=')
  7. config[key.strip()] = value.strip()
  8. return config
  9. config = read_config('config.txt')
  10. print(config)

总结

通过本章的学习,我们掌握了 Python 中文件操作的基本方法,包括文件的读写、文件与目录的创建、删除与重命名以及路径操作与文件检查。在实际编程过程中,文件操作是一个非常常见的需求,熟练掌握这些操作将有助于提高我们的编程能力。

在下一章中,我们将深入学习异常处理,了解如何在代码中优雅地处理各种异常情况,确保程序的健壮性和稳定性。