import string fname = input('Ingresa el nombre de archivo: ') try: fhand = open(fname) except: print('El archivo no se puede abrir:', fname) exit() counts = dict() for line in fhand: line = line.rstrip() line = line.translate(line.maketrans('', '', string.punctuation)) line = line.lower() words = line.split() for word in words: if word not in counts: counts[word] = 1 else: counts[word] += 1 print(counts)