FileNotFoundError: [WinError 2] Система не может найти указанный файл: найдите расширения в os.listdirPython

Программы на Python
Гость
FileNotFoundError: [WinError 2] Система не может найти указанный файл: найдите расширения в os.listdir

Сообщение Гость »


Попытка удалить все файлы, кроме одного, в данном каталоге. Посмотрел другие похожие сообщения, но мой вопрос, похоже, касается получения имен из os.listdir и необходимости полного пути с расширениями для использования os.remove:

Код: Выделить всё

# delete files from the save path but preserve the zip file
if os.path.isfile(zip_path):
for clean_up in os.listdir(data_path):
if not clean_up.endswith(tStamp+'.zip'):
os.remove(clean_up)
Gives this error:

Код: Выделить всё

Line 5:     os.remove(clean_up)

FileNotFoundError: [WinError 2] The system cannot find the file specified: 'firstFileInListName'
I think this is because os.listdir is not capturing the file extension of each file (printed os.listdir(data_path) and only got names of the files without extensions)
What can I do to delete all files from the data_path except for the one that ends with tStamp+'.zip' ?


Источник: https://stackoverflow.com/questions/781 ... ified-find

Вернуться в «Python»