如何在 Python 中将整个文件读入缓冲区并作为字符串返回?
Python 的文件类的 read 函数会自动管理这个。在 Python 中打开一个文件并在文件句柄上调用 read 函数时,它会将整个文件读入字符串并返回该字符串。
更多Python相关文章,请阅读:Python 教程
示例
with open('my_file.txt', 'r') as f:
file_content = f.read() # 将整个文件读入 file_content 字符串
print(file_content)