I am working on a program where i need to count every token(letters,numbers,symbols,and etc) in a text file however when i try to use the len function on the whole file it displays TypeError: object of type '_io.TextIOWrapper' has no len()
My question is basically how would you count every token in a text file
def getCountry(Filename):
o = open((Filename),'r')
return o
def menu(Option,Entry):
if Option == 'A':
J = len(Entry)
return J
if Option == 'B':
num = 0
for line in Entry:
found = sum(line.count(xx) for xx in ('and','del','from','not','while','as','elif', 'global','or','with','assert','else', 'if','pass','yield','break','except','import','print','class','exec','in','rise','continue','finally','is', 'return', 'def', 'for', 'lambda', 'try'))
num = line.split()
num2 = len(num)
Per = ("%.2f" % (found/num2*100))
Convert = (Per,"Percent")
return Convert
if Option == 'C':
num_chars = 0
for line in Entry:
found = sum(line.count(xx)for xx in ('+','-','*','/','.','!','@','#','$','%','^','&','(',')','=','?'))
num_chars= len(line)
Per = found/num_chars
return Per
if Option == 'E':
for line in Entry:
Space = line.count(' ')
return Space
def main():
Filename = input('Input filename: ')
Entry = getCountry(Filename)
print('Now from the options below choose a letter )# list of choices')
print('A)Counts the number of tokens (word, symbols, numbers)')
print('B)Counts the number of selected Python key word (e.g. if, while, …)')
print('and returns the % of tokens that are key)')
print('C)Counts the number of selected programming symbols (e.g. +, : , …) and returns the % of tokens that are symbols')
print('D)Receives the metrics for a program and returns a score that compares the two programs metrics')
print('E) Counts all of the white spaces in program')
Option = input('Enter Letter for option: ')
while Option not in ('A', 'B', 'C','D','E'):#input validation statement
Option = str(input('Enter Capital Letter: '))
Answer2 =menu(Option,Entry)
print(Answer2)
main()