forked from winner9871/the-python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfile_viewer.py
32 lines (22 loc) · 823 Bytes
/
file_viewer.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
29
30
31
import os.path
def main():
fname = input("Enter The Name Of File:")
while not os.path.exists(fname):
print(fname, "Doesn't Exists, Kindly Enter a valid file name")
fname = input("Enter The Name Of File:")
with open(fname, "rt") as fobj:
lines = fobj.readlines()
numOfLines = len(lines)
print(f"There are {numOfLines} lines in {fname}")
print("Enter Line Number You Want To See")
print("Anytime Enter 0 to exit")
while True:
lineNum = int(input("Enter The Line Number :\t"))
if lineNum == 0:
break
elif lineNum > numOfLines:
print(f"Sorry But there are only {numOfLines} lines in {fname}")
else:
print(f"{lineNum:4d} {lines[lineNum - 1]}")
if __name__ == "__main__":
main()