Python Forum
Thread Rating:
  • 2 Vote(s) - 3 Average
  • 1
  • 2
  • 3
  • 4
  • 5
readline() and readlines()
#7
iFunKtion and metulburr,

Thanks for the explanations. Indeed, I have changed to code to use a WHILE loop. Here is the new code:
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 one_line = "Anything, just not null :-)"
10 #for one_line in inp.readline() :
11 while one_line :
12   one_line = inp.readline()
13   one_line = one_line.rstrip('\n')
14   ll = len(one_line)
15   if ll < 1 :
16     break
17   lc += 1 # Not null: Bump up lines-read count
18   print "Input line[%d] is %d chars: %s" % (lc, ll, one_line)
19
20 # Done printing...
21 #
22 inp.close
And voila! the output matches exactly what the readlines() version produced.  Actually, slightly better, since the new version skips the final blank line that I got from readlines().

So now I can mark this thread as "Answered" but I don't see that option. Thanks SO much, folks, for clarifying a confusing basic issue!
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,562 Apr-11-2023, 12:44 AM
Last Post: deanhystad
  Greek letters with .readline() and tkinter KinkgOfKeks 7 3,526 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,492 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,189 Jan-18-2022, 11:05 AM
Last Post: zzzhhh
  [Solved] Using readlines to read data file and sum columns Laplace12 4 5,124 Jun-16-2021, 12:46 PM
Last Post: Laplace12
  readline inside and outside functions paul18fr 2 2,704 May-20-2021, 01:15 PM
Last Post: csr
  Problem with readlines() and comparisons dudewhoneedshelp 2 2,855 Jul-23-2020, 10:21 AM
Last Post: DeaD_EyE
  TypeError: file must have 'read' and 'readline' attributes hobbyist 6 14,764 Jun-12-2020, 05:12 PM
Last Post: DreamingInsanity
  [Python3] Trailing newline in readlines. LWFlouisa 4 6,529 Mar-10-2020, 09:57 AM
Last Post: perfringo
  problem with readline() schlundreflex 6 6,406 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