In Python, say I have:
f = open("file.txt", "r")
a = f.readlines()
b = f.readline()
print a
print b
print a
will show all the lines of the file and print b
will show nothing.
Similarly vice versa:
f = open("file.txt", "r")
a = f.readline()
b = f.readlines()
print a
print b
print a
shows the first line but print b
will show all lines except the first one.
If both a
and b
are readlines(), a
will show all the lines and b
will show nothing.
Why does this happen? Why can't both commands work independently of each other? Is there a workaround for this?