JustCaptain kod-buduschego
1b7ae631e8e0aa08482b2abc32064a70d25c5de5
Перейти к файлу
Код
main.py
# version 1
def count_string_to_file(path: str = "example.txt") -> None:
    file = open(file=path, mode="r", encoding="utf-8")
    count: int = len(file.readlines())
    file.close()
    print(count)


# version 2
def count_string_to_file_2(path: str = "example.txt") -> None:
    file = open(file=path, mode="r", encoding="utf-8")
    count: int = 0
    for _ in range(len(file.readlines())):
        count += 1
    file.close()
    print(count)


# version 3
def count_string_to_file_3(path: str = "example.txt") -> None:
    file = open(file=path, mode="r", encoding="utf-8")
    data: str = file.read()
    count: int = data.count("\n")
    file.close()
    print(count)


# version 4
def count_string_to_file_4(path: str = "example.txt") -> None:
    file = open(file=path, mode="r", encoding="utf-8")
    data: str = file.read()
    file.close()
    count: int = 0
    index: int = 0
    while index != len(data):
        if "{}".format(data[index]) == "\n":
            count += 1
        index += 1
    print(count)


count_string_to_file()
count_string_to_file_2()
count_string_to_file_3()
count_string_to_file_4()