首页 > 解决方案 > 在 Mac 上删除目录的 Python 脚本

问题描述

尝试制作简单的 python 脚本来删除 Mac 上的消息(用于 iMessage)目录。路径是 usrs/library/messages (我想删除messages文件夹)

#!/usr/bin/python

import subprocess, os, shutil`
import send2trash as trash

subprocess.call(['osascript', '-e', 'tell application "Messages" to quit'])

shutil.rmtree('Usr/Library/Messages')

#trash.send2trash(os.path.join("Usr/Library/Messages", "chat.db"))

warning = input("Do you want to restart computer?(y/n)")
if warning == "n":
    exit()
else:
    os.system("shutdown /r /t 1")`import subprocess, os, shutil

在过去的几个小时里,我已经对此进行了彻底的研究(我对编程很陌生)并且很困惑。显示的代码只是删除目录的最新尝试。我也有 send2trash 模块,我无法弄清楚大声笑在本质上堆栈上有很多类似的问题,但没有一个可以帮助我解决这个问题。目前我的代码错误与此消息:

Traceback (most recent call last):
File "/Users/me/PycharmProjects/plistscript/script.py", line 8, in <module>
shutil.rmtree('Usr/Library/Messages')

File  "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/shutil.py", line 494, in rmtree
return _rmtree_unsafe(path, onerror)

File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/shutil.py", line 376, in _rmtree_unsafe
onerror(os.listdir, path, sys.exc_info())

File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/shutil.py", line 374, in _rmtree_unsafe
names = os.listdir(path)

FileNotFoundError: [Errno 2] No such file or directory: 'Usr/Library/Messages'

这是我在这里的第一个问题,所以请放轻松:)

标签: pythonmacosdirectory

解决方案


可能是您要删除/Users/Library/Messages/usr/Library/Messages检查代码中的目录是否存在


推荐阅读