- 使用
open()
函数和read()
方法
这是最基本的方法之一,它使用内置的 open()
函数打开文件并返回一个文件对象,然后可以使用该文件对象的 read()
方法读取文件的全部内容。
file_path = 'path/to/file.txt' # 文件路径
with open(file_path, 'r', encoding='utf-8') as file:
content = file
open()
函数和 read()
方法这是最基本的方法之一,它使用内置的 open()
函数打开文件并返回一个文件对象,然后可以使用该文件对象的 read()
方法读取文件的全部内容。
file_path = 'path/to/file.txt' # 文件路径
with open(file_path, 'r', encoding='utf-8') as file:
content = file