'''读取文件最后一行'''import os# 小文件:批量读取def get_last_line(filename='mark.csv'): fullfilename = os.path.join(os.path.dirname(__file__), filename) with open(fullfilename, 'r', encoding='utf-8') as f: lines = f.readlines() # 批量 lastline = lines[-1] return lastline # 大文件:逐行读取def get_last_line2(filename='mark.csv'): fullfilename = os.path.join(os.path.dirname(__file__), filename) with open(fullfilename, 'r', encoding='utf-8') as f: lastline = f.readline() # 第一行 while lastline: line = f.readline() # 逐行 if not line: break lastline = line return lastlineif __name__ == '__main__': print(get_last_line()) #print(get_last_line2())