python - 在 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'
这是我在这里的第一个问题,所以请放轻松:)
解决方案
可能是您要删除/Users/Library/Messages
或/usr/Library/Messages
检查代码中的目录是否存在
推荐阅读
- vue.js - 更广泛的修改 terser-webpack-plugin?
- php - 如何将一个类中的对象接受到该类的子类中
- jquery - 一个js api中的jquery geocomplete和markerclusterer包括
- javascript - 如何一次从多个表单中检索值
- c++ - 函数的隐式声明在 gcc 中编译,但在 g++ 中不编译
- python - Python:使用 h5py 和 NumPy 从 MATLAB .mat 文件中读取 str 问题
- javascript - 反应并使用 ReactDOM.createPortal() - 目标容器不是 DOM 元素错误
- arrays - 如何从 .select 找到的数组元素中获取字符串,而 ruby 中没有括号?
- android - 如何在执行 AsyncTask 之前强制更新 UI?
- json - 使用 jq:仅选择具有特定子键的父母