uaveiro-leci/1ano/fp/aula07/countLetters.py

27 lines
462 B
Python
Raw Normal View History

2022-11-09 14:52:30 +00:00
import sys
def main():
# Read the file
f = open(sys.argv[1], 'r')
text = f.read()
f.close()
# Count the letters
letters = {}
for c in text:
if c.isalpha():
c = c.lower()
if c in letters:
letters[c] += 1
else:
letters[c] = 1
# Print the results
for c in sorted(letters.keys()):
print(c, letters[c])
if __name__ == "__main__":
main()