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


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM