将刚刚创建好的文件打开并读取内容:1一次读取全部内容2每次读取一行
(1) 一次读取全部内容:
with open('test.txt', 'r') as file:
content = file.read()
print(content)
输出结果:
Hello, world!
This is a test file.
Hope it works well.
(2) 每次读取一行:
with open('test.txt', 'r') as file:
for line in file:
print(line.strip())
输出结果:
Hello, world!
This is a test file.
Hope it works well.
原文地址: https://www.cveoy.top/t/topic/eJfb 著作权归作者所有。请勿转载和采集!