需要删除包含特定单词的每个文件

Need to delete every file that includes a specific word

我正在尝试创建一个按钮来删除所有包含“cast”一词的文件,因为我有很多包含该词的文件,但都具有不同的前缀,例如“(副本 1)”。

import tkinter as tk
import os

root = tk.Tk()
root.title("Matt le blanc terminator")
root.geometry('400x250')

def terminate():
    if os.path.exists('cast.*'):
        os.remove('cast.*')
    else:
        print("File does not exist")
        
TBtn = tk.Button(text="TERMINATE",fg="red", bg="black", command=terminate, width=100, height=50)
TBtn.pack()

root.mainloop()

如果名称中包含“cast”,则列出目录然后检查每个文件可能更简单

import os 
all_files = os.listdir()
for f in all_files:
    file_without_extension = os.path.splitext(f)[0]
    if "cast" in file_without_extension:
        os.remove(f)