本文實例敘述了Python按行讀取檔案的實作方法。分享給大家供大家參考,具體如下:
小檔案:
##coding=utf-8 #author: walker #date: 2013-12-30 #function: 按行读取小文件 all_lines = [] try: file = open('txt.txt', 'r') all_lines = file.readlines() except IOError as err: print('File error: ' + str(err)) finally: if 'file' in locals(): file.close() for line in all_lines: print(line)
大檔案:
#coding=utf-8 #author: walker #date: 2013-12-30 #function: 按行读取大文件 try: file = open('txt.txt', 'r') for line in file: print(line) except IOError as err: print('File error: ' + str(err)) finally: if 'file' in locals(): file.close()
更多Python按行讀取檔案的實作方法【小檔案與大檔案讀取】相關文章請關注PHP中文網!
#