Добавлены файлы data.txt, main.py
This commit is contained in:
commit
75ab10b872
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
@ -0,0 +1 @@
|
||||
output.txt
|
7
.vscode/launch.json
vendored
Normal file
7
.vscode/launch.json
vendored
Normal file
@ -0,0 +1,7 @@
|
||||
{
|
||||
// Use IntelliSense to learn about possible attributes.
|
||||
// Hover to view descriptions of existing attributes.
|
||||
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
|
||||
"version": "0.2.0",
|
||||
"configurations": []
|
||||
}
|
4
data.txt
Normal file
4
data.txt
Normal file
@ -0,0 +1,4 @@
|
||||
Жили у бабуси 3.5 веселых гуся: один серый другой белый следующий синий поеследний оранж.
|
||||
|
||||
Дед да бабка репу чесали какое заадние придумать да не придумали и решили они посчитать сколько определенных символов в этом тексте да считать не умели,
|
||||
зато умели писать кодик на питончике ,он им и посчитал сколько и чего тут содержится.
|
43
main.py
Normal file
43
main.py
Normal file
@ -0,0 +1,43 @@
|
||||
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()
|
1
Задание.txt
Normal file
1
Задание.txt
Normal file
@ -0,0 +1 @@
|
||||
Считает кол-во заданного символа и записывает результаты в файл , счиатет запятые и точки ,пробелы.
|
Loading…
Reference in New Issue
Block a user