Python/main.py

44 lines
1.6 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import os
def count_character_and_spaces(file_path, output_path, char_to_count):
try:
if not os.path.exists(file_path):
print("Ошибка: Файл data.txt не найден. Проверьте путь.")
return
with open(file_path, 'r', encoding='utf-8') as file:
content = file.read()
char_count = content.count(char_to_count)
space_count = content.count(' ')
dot_count = content.count('.')
comma_count = content.count(',')
result = (f"Символ '{char_to_count}' встречается {char_count} раз(а).\n"
f"Количество пробелов: {space_count}\n"
f"Количество точек: {dot_count}\n"
f"Количество запятых: {comma_count}\n")
with open(output_path, 'w', encoding='utf-8') as output_file:
output_file.write(result)
print("Результат записан в output.txt")
except Exception as e:
print(f"Произошла ошибка: {e}")
def main():
file_path = os.path.join(os.environ['USERPROFILE'], 'Desktop', 'рактика2', 'data.txt')
output_path = os.path.join(os.environ['USERPROFILE'], 'Desktop', 'рактика2', 'output.txt')
char_to_count = input("Введите символ для поиска: ")
if len(char_to_count) != 1:
print("Ошибка: Нужно ввести ровно один символ.")
return
count_character_and_spaces(file_path, output_path, char_to_count)
if __name__ == "__main__":
main()