Answer the question
In order to leave comments, you need to log in
How to fix this bug?
There is a code:
if __name__ == "__main__":
path = input('Введите имя файла:')
a = open(path)
act = input('Какое действие хотите выполнить с файлом? \n 1. Подсчитать кол-во строк \n 2. Подсчитать кол-во символов в файле \n 3. 1 + 2: ')
def countLines(name):
count = 0
for l in name.readlines():
count += 1
return count
def countChars(name):
b = len(name.read())
return b
def test(name):
print(countLines(name))
print(countChars(name))
return "Done"
if __name__ == "__main__":
if act == "1":
print(countLines(a))
elif act == "2":
print(countChars(a))
elif act == "3":
print(test(a))
else:
print("Ошибка")
Answer the question
In order to leave comments, you need to log in
When you have counted the lines in a file, you have read it to the end. After that, the call to read() will return EOF, i.e. in fact, no more characters can be read. To read the file again, you need to call seek(0) (well, or write another method that counts both lines and characters at the same time).
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question