PY
py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
fh = open("/usercode/files/books.txt")
# readliens(), returns a list of lines
lines = fh.readlines()
print(type(lines))
print(lines)
# seek(0), move the 'cursor' back to beginning
fh.seek(0)
print(f'\n{"*" * 40}')
# readline(), read one line at a time
for i in range(len(lines)):
print(fh.readline())
# There is a extra line because each line contains \n and print ends by default ends with \n too. So it prints 'Harry Potter\n\n'
fh.seek(0)
print(f'\n{"*" * 40}')
# readline(), using strip to remove extra line
for i in range(len(lines)):
print(fh.readline().strip())
# varient of readline and readlines
# We can specific how many character / lines to read by passing an argument
fh.seek(0)
Enter to Rename, Shift+Enter to Preview
OUTPUT
Run