Python Forum
Thread Rating:
  • 2 Vote(s) - 3 Average
  • 1
  • 2
  • 3
  • 4
  • 5
readline() and readlines()
#4
readline is the least used. You would only use it in the event the file was too large that readlines() is too much to put into RAM or too slow to do so. And then you would most likely use a database instead. You can also control how many bytes are read if you give it an argument.

You wouldnt use readline in a for loop. Something like this
f = open('text.txt')
line = f.readline()
while line:
    print(line, end='')
    line = f.readline()
where readlines would be
with open('text.txt') as f:
    lines = f.readlines()
Even if you wanted to read the first 5 lines of a file would would just splice readlines()
Recommended Tutorials:
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,567 Apr-11-2023, 12:44 AM
Last Post: deanhystad
  Greek letters with .readline() and tkinter KinkgOfKeks 7 3,532 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,525 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,193 Jan-18-2022, 11:05 AM
Last Post: zzzhhh
  [Solved] Using readlines to read data file and sum columns Laplace12 4 5,131 Jun-16-2021, 12:46 PM
Last Post: Laplace12
  readline inside and outside functions paul18fr 2 2,714 May-20-2021, 01:15 PM
Last Post: csr
  Problem with readlines() and comparisons dudewhoneedshelp 2 2,858 Jul-23-2020, 10:21 AM
Last Post: DeaD_EyE
  TypeError: file must have 'read' and 'readline' attributes hobbyist 6 14,773 Jun-12-2020, 05:12 PM
Last Post: DreamingInsanity
  [Python3] Trailing newline in readlines. LWFlouisa 4 6,536 Mar-10-2020, 09:57 AM
Last Post: perfringo
  problem with readline() schlundreflex 6 6,419 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