CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
jackfrued

CoCalc provides the best real-time collaborative environment for Jupyter Notebooks, LaTeX documents, and SageMath, scalable from individual users to large groups and classes!

GitHub Repository: jackfrued/Python-100-Days
Path: blob/master/Day01-15/code/Day11/file1.py
Views: 729
1
"""
2
从文本文件中读取数据
3
4
Version: 0.1
5
Author: 骆昊
6
Date: 2018-03-13
7
"""
8
9
import time
10
11
12
def main():
13
# 一次性读取整个文件内容
14
with open('致橡树.txt', 'r', encoding='utf-8') as f:
15
print(f.read())
16
17
# 通过for-in循环逐行读取
18
with open('致橡树.txt', mode='r') as f:
19
for line in f:
20
print(line, end='')
21
time.sleep(0.5)
22
print()
23
24
# 读取文件按行读取到列表中
25
with open('致橡树.txt') as f:
26
lines = f.readlines()
27
print(lines)
28
29
30
if __name__ == '__main__':
31
main()
32
33