Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 4469
  • Last Modified:

Python + Mac: os.remove = Operation not permitted?

It's simple: I have created a folder on the desktop that I later want to remove. But when I run the script, I get this:

Processing order from queue:  002508
Copying order to desktop: /Volumes/process/002508 -> /Users/Michael/Desktop/002508 
Traceback (most recent call last):
  File "./process-queue.py", line 64, in <module>
    prep_desktop()
  File "./process-queue.py", line 25, in prep_desktop
    os.remove(thefolder)
OSError: [Errno 1] Operation not permitted: '/Users/Michael/Desktop/TheFolder'

Open in new window


The file is owned by Michael:staff, and I can delete it manually by dragging into the trash can. I can delete it with rm -vf. I just can't delete it from within Python.

Please advise.
0
DrDamnit
Asked:
DrDamnit
1 Solution
 
farzanjCommented:
So you are trying

os.remove('/Users/Michael/Desktop/TheFolder')

Open in new window


Can you try this?
os.rmdir("/Users/Michael/Desktop/TheFolder")

Open in new window


Or this:
import shutil

shutil.rmtree('/Users/Michael/Desktop/TheFolder')

Open in new window

0
 
peprCommented:
A side note: The single or double quotes can be used interchangeably. They have the same functionality.

My +1 for shutil.rmtree, only the folder must not be a symblolic link. The os.rmdir works only for the empty directory. The os.remove can remove only the files.
0
 
DrDamnitAuthor Commented:
Rmtree is the winner.
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now