Repository navigation
Expand file tree
/
Copy pathwordcount.py
More file actions
51 lines (42 loc) · 1.59 KB
/
Copy pathwordcount.py
File metadata and controls
51 lines (42 loc) · 1.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# Read a file, count vowels and unique letter types (case-insensitive).
# Relative paths use the process current directory (where pythonami was started).
def main():
filename = input("Enter the filename to read: ")
try:
with fopen(filename, 'r') as f:
# Large count means "up to EOF"; runtime sizes the buffer from the
# remaining file length (not the count), so this is Amiga-safe.
text = fread(f, 2147483647)
except OSError:
print("Error reading the file.")
return
except ValueError:
print("Error reading the file.")
return
vowels = "aeiouAEIOU"
vowel_count = 0
letters_found = set()
for char in text:
if char.isalpha():
letters_found.add(char.lower())
if char in vowels:
vowel_count += 1
print("Number of vowels found:", vowel_count)
print("Number of different letter types found:", len(letters_found))
# Dictionary to count occurrences of each letter (case insensitive)
letter_counts = {}
for char in text:
if char.isalpha():
char_lower = char.lower()
if char_lower in letter_counts:
letter_counts[char_lower] += 1
else:
letter_counts[char_lower] = 1
# Output results (f-strings are not in Language Level 0.1)
if letter_counts:
print("Letters found in the file and their counts:")
for letter, count in sorted(letter_counts.items()):
print("'" + letter + "':", count)
else:
print("No letters found in the file.")
main()