Python NOTES 4 File Handling
# gives total number of lines in a txt file using termination as \n. i.e. everytime python encounters EOF, it adds 1 to the list.
def lineNumber():
lines = sum(1 for i in open('Dairy.txt'))
return lines
# data -> line number
lookup = 'tejas'
with open("Dairy.txt") as myFile:
for num, line in enumerate(myFile, 1):
if lookup in line.lower():
print('found at line:', num)
# Line number -> data
lineNumber = 11
with open("Dairy.txt") as f:
for searchline, data in enumerate(f, 1):
if searchline == lineNumber:
print(data, end="")
""" ENUMERATE(data, number) is a type of inbuilt function that gives item along with its index from a list(item, index). Here, each line in txt file represents as a list to python. """

Comments
Post a Comment