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