python删除文件一般使用os.remove,但这样删是直接删除文件,不删到回收站的,那么想删除文件到回收站怎么办?
这时,就需要使用shell模块了
from win32com.shell import shell,shellcon debug=False def deltorecyclebin(filename): print('deltorecyclebin', filename) # os.remove(filename) #直接删除文件,不经过回收站 if not debug: res= shell.SHFileOperation((0,shellcon.FO_DELETE,filename,None, shellcon.FOF_SILENT | shellcon.FOF_ALLOWUNDO | shellcon.FOF_NOCONFIRMATION,None,None)) #删除文件到回收站 if not res[1]: os.system('del '+filename)
注:filename为图片路径,例:C:UsersAdministratorDesktop est1out 00097.jpg
关于SHFileOperation的用法,请移步:https://www.cnblogs.com/xiaodai0/p/10174877.html