Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python3
- # -*- coding: utf-8 -*-
- # Filename: consonants_count.py
- # Author: Jeoi Reqi
- """
- Consonants Count Script
- This script reads text from a specified file in the same directory and counts
- the number of consonants in the text. It utilizes a function, count_consonants,
- to perform the consonant counting operation.
- Requirements:
- - Python 3
- Usage:
- 1. Save the text file in the same directory as the script.
- 2. Replace 'texty.txt' with the actual file name in the script.
- 3. Run the script.
- 4. The script will display the number of consonants in the text.
- """
- def count_consonants(text):
- """Count the number of consonants in a text.
- Args:
- text (str): Text.
- Returns:
- int: Number of consonants in the text.
- """
- consonants = set("bcdfghjklmnpqrstvwxyz")
- return sum(char.lower() in consonants for char in text)
- def read_text_from_file(file_path):
- """Read text from a file.
- Args:
- file_path (str): Path to the file.
- Returns:
- str: Text read from the file.
- """
- with open(file_path, 'r', encoding='utf-8') as file:
- return file.read()
- def main():
- # Specify the file path
- file_path = 'texty.txt' # Replace 'texty.txt' with the actual file name
- # Read text from the file
- text = read_text_from_file(file_path)
- # Count consonants
- consonant_count = count_consonants(text)
- # Display the result
- print(f"Number of consonants in the text: {consonant_count}")
- if __name__ == "__main__":
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement