-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfile_stats.py
More file actions
67 lines (56 loc) · 2.03 KB
/
Copy pathfile_stats.py
File metadata and controls
67 lines (56 loc) · 2.03 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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
def is_vowel(c):
'''Returns True if the character c is a vowel.'''
return c.lower() in 'aeiou'
def is_consonant(c):
'''Returns True if the character c is a consonant.'''
return c.isalpha() and not is_vowel(c)
def is_digit(c):
'''Returns True if the character c is a digit.'''
return c.isdigit()
def is_whitespace(c):
'''Returns True if the character c is a whitespace character.'''
return c in ' \t\n\r'
def collect_statistics(filename):
'''Collects the relevant statistics from the file.'''
num_lines = 0
consonants = 0
vowels = 0
digits = 0
whitespace = 0
longest_line_length = 0
total_characters = 0
for line in filename:
num_lines += 1
line_length = len(line)
total_characters += line_length
if line_length > longest_line_length:
longest_line_length = line_length
for char in line:
if is_vowel(char):
vowels += 1
elif is_consonant(char):
consonants += 1
elif is_digit(char):
digits += 1
elif is_whitespace(char):
whitespace += 1
print_statistics(num_lines, total_characters, consonants, vowels, digits, whitespace, longest_line_length)
def print_statistics(lines, total_characters, consonants, vowels, digits, whitespace, longest_line):
'''Prints the file statistics.'''
print("Number of Lines:", lines)
print("Total Characters:", total_characters)
print("Vowels:", vowels)
print("Consonants:", consonants)
print("Digits:", digits)
print("Whitespace Characters:", whitespace)
print("Length of Longest Line:", longest_line)
def main():
'''Prompts the user for a filename and gathers statistics.'''
filename = input('Please enter the name of the file: ')
try:
with open(filename, 'r') as file:
print('\n**** FILE SUMMARY ****\n')
collect_statistics(file)
except FileNotFoundError:
print(f"Error: The file '{filename}' was not found.")
main()