Solved

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

Posted on 2014-04-21
3
2,825 Views
Last Modified: 2014-04-21
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
Comment
Question by:DrDamnit
3 Comments
 
LVL 31

Accepted Solution

by:
farzanj earned 500 total points
ID: 40012575
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
 
LVL 28

Expert Comment

by:pepr
ID: 40013041
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
 
LVL 32

Author Closing Comment

by:DrDamnit
ID: 40013290
Rmtree is the winner.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Strings in Python are the set of characters that, once defined, cannot be changed by any other method like replace. Even if we use the replace method it still does not modify the original string that we use, but just copies the string and then modif…
In this article we will discuss some EI Capitan Mail app issues and provide some manual process to resolve them.
Learn the basics of if, else, and elif statements in Python 2.7. Use "if" statements to test a specified condition.: The structure of an if statement is as follows: (CODE) Use "else" statements to allow the execution of an alternative, if the …
Learn the basics of while and for loops in Python.  while loops are used for testing while, or until, a condition is met: The structure of a while loop is as follows:     while <condition>:         do something         repeate: The break statement m…

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now