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:\Users\Administrator\Desktop\test1\out\000097.jpg
關於SHFileOperation的用法,請移步:https://www.cnblogs.com/xiaodai0/p/10174877.html
