Python 3如何删除文件夹中的图像

use*_*181 1 delete-file python-3.x

如何使用Python 3删除文件夹中的所有png格式图片?

zea*_*soi 9

这种单一的行语句将每个文件在指定的路径,并删除它,如果文件名结尾的.png:

import os
os.remove(file) for file in os.listdir('path/to/directory') if file.endswith('.png')
Run Code Online (Sandbox Code Playgroud)

  • 你好,我正在使用 python 3.8。出现错误:语句必须用换行符或分号分隔。 (2认同)