import string fname = input('Podaj nazwę pliku: ') try: fhand = open(fname) except: print('Nie można otworzyć pliku:', fname) exit() counts = dict() for line in fhand: line = line.translate(str.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)