F
F
FIRSTNAME LASTNAME2019-08-14 18:32:04
Python
FIRSTNAME LASTNAME, 2019-08-14 18:32:04

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('Указанная директория не найдена!')
5d54290c534c0908525638.png

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
Roman Kitaev, 2019-08-14
@true_pelmeshek

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 question

Ask a Question

731 491 924 answers to any question