English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
This article illustrates the implementation method of Python reading files by line. Share it with everyone for reference as follows:
Small file:
#coding=utf-8 #author: walker #date: 2013-12-30 #function: Read small file by line 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)
Large file:
#coding=utf-8 #author: walker #date: 2013-12-30 #function: Read large file by line 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()
Readers who are interested in more content related to Python can check the special topics on this site: 'Summary of Python File and Directory Operation Skills', 'Summary of Python Text File Operation Skills', 'Summary of Python URL Operation Skills', 'Summary of Python Image Operation Skills', 'Python Data Structures and Algorithms Tutorial', 'Summary of Python Socket Programming Skills', 'Summary of Python Function Usage Skills', 'Summary of Python String Operation Skills', and 'Classic Tutorial of Python Entry and Advanced Learning'.
I hope the content described in this article will be helpful to everyone in Python program design.
Declaration: The content of this article is from the Internet, and the copyright belongs to the original author. The content is contributed and uploaded by Internet users spontaneously. This website does not own the copyright, has not been manually edited, and does not assume any relevant legal liability. If you find any content suspected of copyright infringement, please send an email to: notice#w3Please report via email to codebox.com (replace # with @) and provide relevant evidence. Once verified, this site will immediately delete the infringing content.