Methods for reading files and lines of files.
Read files in Python.
Date Created:Friday December 29th, 2006 03:41 AM
Date Modified:Friday August 01st, 2008 02:04 AM
# basic way to print out a file: >>> file = open('readme.txt','r') >>> print file.read() # prints out each character of a file followed by a new line: >>> file = open('readme.txt') >>> while 1: ... char = file.read(1) ... if not char: break ... print char # same thing with less lines: >>> for char in open('readme.txt').read(): ... print char # prints lines of a file: >>> file = open('readme.txt') >>> while 1: ... line = file.readline() ... if not line: break ... print line # same thing, less code: >>> for line in open('readme.txt').readlines(): ... print line >>> for x in open('readme.txt').xreadlines(): ... print x