try: filename = 'file.txt' fobj = open(filename, 'r') for eachLine in fobj: print eachLine, fobj.close() except IOError, e: print 'file open error:', e