-2

Trying to make simple python script to delete the messages (for iMessage) directory on Mac. Path is usrs/library/messages (I want to delete messages folder)

#!/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

I have thoroughly researched this for the past few hours (I'm pretty new to programming) and am sooo stumped. The code shown is only the latest attempt to delete a directory. I also have the send2trash module that I couldn't figure out lol there were plenty of questions similar on stack in nature but none that helped me enough to crack this. Currently my code errors with this message:

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'

This is my first question on here so go easy on me :)

ncooke3
  • 548
  • 3
  • 10

1 Answers1

0

may be you want remove /Users/Library/Messages or /usr/Library/Messages,check the directory in your code is exist

Kr.98
  • 106
  • 8
  • good idea. I have checked to make sure there is a folder there and there definitely is. it is full of junk files and what not but I can cd into it so it very much exists @Kr.98 – ncooke3 Sep 30 '18 at 02:33
  • you may use an absolute path,so add `/` before the path,try `shutil.rmtree('/Usr/Library/Messages')` – Kr.98 Sep 30 '18 at 02:51
  • `shutil.rmtree('Usr/Library/Messages')` will try to remove tree from where the script your run it ,ie, if run your script in `/Home/Lee/` ,it will try to remove `/Home/Lee/Usr/Library/Messages`@ncooke3 – Kr.98 Sep 30 '18 at 02:55
  • okay! good news and bad news. I believe adding the "/" first got us to the correct directory. now the error is "PermissionError: [Errno 1] Operation not permitted" any idea on how to grant permission on Mac? I ran `$ ls -l /Users/library/messages` and it returned `ls: messages: Operation not permitted` @Kr.98 – ncooke3 Sep 30 '18 at 03:12
  • @ncooke3 you may need sudo Permission,try to run it with `sudo python xxx.py` – Kr.98 Sep 30 '18 at 03:22
  • just tried that. prompted for password but gave me same permission error. what in the world.... this may help too https://stackoverflow.com/a/33922653/9331576 @Kr.98 – ncooke3 Sep 30 '18 at 03:26
  • try run `sudo rm /Usr/Library/Messages/‘ with cli ,if prompt same permission error,may be you should disable mac sip – Kr.98 Sep 30 '18 at 03:43
  • result: `rm: /Users/nickcooke/library/messages: is a directory ` and the folder is still there @Kr.98 – ncooke3 Sep 30 '18 at 04:19
  • @ncooke3 `rm -rf /Users/nickcooke/library/messages` – Kr.98 Sep 30 '18 at 06:03
  • rm: /Users/nickcooke/library/messages: Operation not permitted @Kr.98 – ncooke3 Sep 30 '18 at 17:59
  • disable mac sip,if you just program for fun,I don't suggest you do that – Kr.98 Oct 01 '18 at 03:18
  • alright thank you ...after looking into it more I believe that will do the trick but since I am just messing around here I dont think that is worth it. perhaps ill stumble upon a workaround but thanks for your help @Kr.98 – ncooke3 Oct 01 '18 at 03:54