python-----删除文件到回收站

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

posted @ 2018-12-25 11:17  小呆丶  阅读(5199)  评论(1编辑  收藏  举报