Advertisement
Python253

file_character_count

Mar 3rd, 2024 (edited)
774
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.28 KB | None | 0 0
  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. # Filename: file_character_count.py
  4. # Author: Jeoi Reqi
  5.  
  6. """
  7. File Character Count Script
  8.  
  9. This script reads text from a specified file ('texty.txt') in the same directory
  10. and counts the number of characters in the text. It utilizes a function,
  11. count_characters, to perform the character counting operation.
  12.  
  13. Requirements:
  14. - Python 3
  15.  
  16. Usage:
  17. 1. Save the 'texty.txt' file in the same directory as the script.
  18. 2. Replace 'texty.txt' with the actual file name in the script if needed.
  19. 3. Run the script.
  20. 4. The script will display the number of characters in the text file.
  21. """
  22.  
  23. def count_characters(file):
  24.     """Count the number of characters in a text file.
  25.  
  26.    Args:
  27.        file (str): Path of a text file.
  28.  
  29.    Returns:
  30.        int: Number of characters in the text file.
  31.  
  32.    """
  33.     with open(file, encoding='utf-8') as f:
  34.         content = f.read()
  35.     return len(content)
  36.  
  37. def main():
  38.     # Specify the file path
  39.     file_path = 'texty.txt'  # Replace 'texty.txt' with the actual file name if needed
  40.  
  41.     # Count characters
  42.     character_count = count_characters(file_path)
  43.  
  44.     # Display the result
  45.     print(f"Number of characters in the text file: {character_count}")
  46.  
  47. if __name__ == "__main__":
  48.     main()
  49.  
  50.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement