#occurences.py #count #occurences of a string in a file fname = input("Name of file: ") #inf = open("C:\\Users\\xyz\\Downloads\\m.html") inf = open(fname) if not inf.readable(): print("Can't read file. Quitting") quit() else: contents = inf.read() s = input("string to count occurences of: ") occurences = 0 s_len = len(s) for i in range(len(contents)): if s == contents[i:i+s_len]: occurences += 1 print("#occurences of",s,"is",occurences)