一、使用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()