python读文件

import fileinput

# fileinput
files = ['./test-re.py']
for line in fileinput.input(files):
    print(line)

#################################
# withopen 读
with open('./test-re.py', 'r') as file:
    for line in file:
        print(line, end='')  # 不加end时,输出的行之间会添加一个空行

#################################
# 分段读
def read_in_chunks(file_path, size=1024 * 1024):
    file_ojb = open(file_path)
    while True:
        chunk_data = file_ojb.read(size)
        if not chunk_data:
            break
        yield chunk_data


file_path = './test-re.py'
for line in read_in_chunks(file_path):
    print(line)
posted @ 2020-03-05 10:59  xj-record  阅读(118)  评论(0编辑  收藏  举报