Reputation: 13
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(), readlines() or iterate over the open file using for.
any suggestions? thanks
Upvotes: 0
Views: 135
Reputation: 414215
You could use a two-argument iter()
version:
lines = iter(f.readline, "")
If you need a list of lines:
lines = list(lines)
Upvotes: 4
Reputation: 4856
First draft:
lines = []
with open(SINGLE_FILENAME) as f:
while True:
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.
Upvotes: 3