更新
This commit is contained in:
33
Python-100-Days/Day01-15/Day11/file1.py
Normal file
33
Python-100-Days/Day01-15/Day11/file1.py
Normal file
@@ -0,0 +1,33 @@
|
||||
"""
|
||||
|
||||
从文本文件中读取数据
|
||||
|
||||
Version: 0.1
|
||||
Author: 骆昊
|
||||
Date: 2018-03-13
|
||||
|
||||
"""
|
||||
|
||||
import time
|
||||
|
||||
|
||||
def main():
|
||||
# 一次性读取整个文件内容
|
||||
with open('致橡树.txt', 'r', encoding='utf-8') as f:
|
||||
print(f.read())
|
||||
|
||||
# 通过for-in循环逐行读取
|
||||
with open('致橡树.txt', mode='r') as f:
|
||||
for line in f:
|
||||
print(line, end='')
|
||||
time.sleep(0.5)
|
||||
print()
|
||||
|
||||
# 读取文件按行读取到列表中
|
||||
with open('致橡树.txt') as f:
|
||||
lines = f.readlines()
|
||||
print(lines)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
Reference in New Issue
Block a user