Python Forum
Thread Rating:
  • 2 Vote(s) - 3 Average
  • 1
  • 2
  • 3
  • 4
  • 5
readline() and readlines()
#1
Greetings.

This is my first thread on this forum. I am learning Python from a book named "Learn to Program Using Python" (I am a VERY experienced programmer and DBA, BTW.) In the chapter on I/O I wasted no time diverging from the book's example.  My code reads a line from a file and then spits it out with a line count and length.  Here is the code, with my line numbers:
  1 #!/usr/bin/python
  2 # files1.py - first program to play with files
  3 #
  4 filename = "justsome.txt"
  5
  6 inp = open(filename, "r")
  7 lc = 0
  8
  9 for one_line in inp.readlines() :
 10   lc += 1     # Bump up lines-read count
 11   one_line = one_line.rstrip('\n')
 12   ll = len(one_line)
 13   print "Input line[%d] is %d chars: %s" % (lc, ll, one_line)
 14
 15 # Done printing...
 16 #
 17 inp.close
Here, the output looks just as expected:
$ ./files1.py

Input line[1] is 22 chars: This is the first line
Input line[2] is 18 chars: and this is second
Input line[3] is 35 chars: I'm a little teapot short and stout
Input line[4] is 36 chars: Pour me out into the forest primeval
Input line[5] is 36 chars: THe murmuing poines and the hemlocks


Next, the book discusses using readline() rather than readlines().  Of course I gotta try that (in line 9). And of course it barfs in my face.  Here is the output of that:
Input line[1] is 1 chars: T
Input line[2] is 1 chars: h
Input line[3] is 1 chars: i
Input line[4] is 1 chars: s
Input line[5] is 1 chars:
Input line[6] is 1 chars: i
Input line[7] is 1 chars: s
Input line[8] is 1 chars:
Input line[9] is 1 chars: t
Input line[10] is 1 chars: h
Input line[11] is 1 chars: e
Input line[12] is 1 chars:
Input line[13] is 1 chars: f
Input line[14] is 1 chars: i
Input line[15] is 1 chars: r
Input line[16] is 1 chars: s
Input line[17] is 1 chars: t
Input line[18] is 1 chars:
Input line[19] is 1 chars: l
Input line[20] is 1 chars: i
Input line[21] is 1 chars: n
Input line[22] is 1 chars: e
Input line[23] is 0 chars:


There are two weirdnesses about this:
  1. It is accepting only one character at a time.  I have googled this issue and found the answers wanting.  And searching this forum with so general a string will get me a haystack when I need the needle.
  2. It stopped reading after the first line.  Otherwise the output would have been a line for every blessed character in the input file.
So my obvious questions are:
  • How do I make readline() read a full line at each call?
  • Why did it stop after the first line?
  • Not that serious here but why isn't the len() function a string method?
Thank you.

-- Rasputin (my middle name is Concise Liar ) Paskudniak
Reply


Messages In This Thread
readline() and readlines() - by rpaskudniak - Nov-17-2017, 12:35 AM
RE: readline() and readlines() - by metulburr - Nov-17-2017, 02:02 AM
RE: readline() and readlines() - by rpaskudniak - Nov-17-2017, 04:11 AM
RE: readline() and readlines() - by metulburr - Nov-17-2017, 04:34 AM
RE: readline() and readlines() - by iFunKtion - Nov-17-2017, 11:33 AM
RE: readline() and readlines() - by buran - Nov-17-2017, 11:44 AM
RE: readline() and readlines() - by rpaskudniak - Nov-17-2017, 07:15 PM
RE: readline() and readlines() - by snippsat - Nov-17-2017, 08:02 PM
RE: readline() and readlines() - by rpaskudniak - Nov-21-2017, 07:20 PM
RE: readline() and readlines() - by metulburr - Nov-21-2017, 07:39 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Pyserial Readline() conversion to array bprosman 1 3,528 Apr-11-2023, 12:44 AM
Last Post: deanhystad
  Greek letters with .readline() and tkinter KinkgOfKeks 7 3,486 Mar-24-2023, 05:13 PM
Last Post: deanhystad
Star Pyserial not reading serial.readline fast enough while using AccelStepper on Arduino MartyTinker 4 8,421 Mar-13-2023, 04:02 PM
Last Post: deanhystad
  readline.parse_and_bind() does not work in start-up script of Python interpreter zzzhhh 0 2,168 Jan-18-2022, 11:05 AM
Last Post: zzzhhh
  [Solved] Using readlines to read data file and sum columns Laplace12 4 5,064 Jun-16-2021, 12:46 PM
Last Post: Laplace12
  readline inside and outside functions paul18fr 2 2,698 May-20-2021, 01:15 PM
Last Post: csr
  Problem with readlines() and comparisons dudewhoneedshelp 2 2,844 Jul-23-2020, 10:21 AM
Last Post: DeaD_EyE
  TypeError: file must have 'read' and 'readline' attributes hobbyist 6 14,730 Jun-12-2020, 05:12 PM
Last Post: DreamingInsanity
  [Python3] Trailing newline in readlines. LWFlouisa 4 6,503 Mar-10-2020, 09:57 AM
Last Post: perfringo
  problem with readline() schlundreflex 6 6,351 Nov-06-2019, 02:22 PM
Last Post: schlundreflex

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020