Answer the question
In order to leave comments, you need to log in
Why does exception handling fail?
I have a small program that iterates through the files in a directory and outputs the resulting list to a file with a .txt extension
. A FileNotFoundError exception occurs if an invalid directory path is specified. However try/except do absolutely nothing. At first I thought that the error was indentation, but everything is exactly as it should be.
import os
file_name = input("Введите имя файла: ")
way = input('Введите полный путь к директории: ')
file = open(file_name, 'w')
file.close()
lst = (os.listdir(way))
try:
for item in lst:
file = open(file_name, 'a')
file.write(item + '\r')
file.close()
except FileNotFoundError:
print('Указанная директория не найдена!')
Answer the question
In order to leave comments, you need to log in
You have an error above, in lst = (os.listdir(way)). You can see it in the traceback. And it is not wrapped in try-except
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question