Python读取文件指定行

By | 2022-01-16

读取指定行

import linecache
file_path = r'D:\work\python\test.txt'
line_number = 5
def get_line_context(file_path, line_number):
    return linecache.getline(file_path, line_number).strip()