使用Python+winshell/shutil清空回收站

一、使用Python中winshell模块的recycle_bin().empty函数清空Windows回收站

        winshell.recycle_bin函数,返回一个ShellRecycleBin对象,利用该对象的empty方法可以清空回收站

empty(confirm=False, show_progress=False, sound=False)

confirm: 如果为True,则清空回收站之前显示确认对话框,默认值为False

show_progress:如果为True,则在清空回收站时显示进度条,默认值为False

sound:如果为True,则在清空回收站时播放声音,默认值为False

import os
import shutil
import platform

# 清空回收站
def empty_recycle_bin():

    # 获取系统类型为Windows系统
    os_name = platform.system()

    if os_name == "Windows":

        # 清空Windows回收站
        try:
            from winshell import recycle_bin

            recycle_bin().empty(confirm=False,show_progress=False, sound=False)

        except ImportError as e:
            print(e)

if __name__ == '__main__':
    empty_recycle_bin()

二、使用Python中shutil.rmtree函数和os.unlink清空macOS废纸篓

import os
import shutil
import platform

# 清空回收站
def empty_recycle_bin():

    # 获取系统类型为MacOS系统
    os_name = platform.system()

    if os_name=='Darwin':

        # 清空macOS废纸篓
        try:
            import glob

            # 获取MacOS系统回收站文件夹和文件列表
            for file in glob.glob(os.path.expanduser('~/.Trash/*')):
                print(file)

                if os.path.isdir(file):

                    # 删除文件夹
                    shutil.rmtree(file)
                else:
                    
                    # 删除文件链接
                    os.unlink(file)

        except OSError as e:
            print(e)

if __name__ == '__main__':
    empty_recycle_bin()

三、使用Python中shutil.rmtree函数和os.unlink清空Linux回收站

import os
import shutil
import platform

# 清空回收站
def empty_recycle_bin():

    # 获取系统类型为Linux系统
    os_name = platform.system()

    if os_name == "Linux":
        # 清空Linux回收站

        try:
            import glob

            # 获取Linux系统回收站文件夹和文件列表
            for file in glob.glob(os.path.expanduser('~/.local/share/Trash/files/*')):
                print(file)

                # 删除文件夹
                if os.path.isdir(file):
                    shutil.rmtree(file)

                else:
                    # 删除文件链接
                    os.unlink(file)

        except OSError as e:
            print(e)

if __name__ == '__main__':
    empty_recycle_bin()

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

梦实学习室

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值