How To Read From A File Using Only Readline()
The code i have right now is this f = open(SINGLE_FILENAME) lines = [i for i in f.readlines()] but my proffessor demands that You may use readline(). You may not use read(), read
Solution 1:
You could use a two-argument iter()
version:
lines = iter(f.readline, "")
If you need a list of lines:
lines = list(lines)
Solution 2:
First draft:
lines = []
withopen(SINGLE_FILENAME) as f:
whileTrue:
line = f.readline()
if line:
lines.append(line)
else:
break
I feel fairly certain there is a better way to do it, but that does avoid iterating with for, using read, or using readlines.
You could write a generator function to keep calling readline()
until the file was empty, but that doesn't really seem like a large improvement here.
Post a Comment for "How To Read From A File Using Only Readline()"