Solved

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

Posted on 2014-04-21
3
3,144 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 29

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

Migrating Your Company's PCs

To keep pace with competitors, businesses must keep employees productive, and that means providing them with the latest technology. This document provides the tips and tricks you need to help you migrate an outdated PC fleet to new desktops, laptops, and tablets.

Question has a verified solution.

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

Suggested Solutions

The purpose of this article is to demonstrate how we can use conditional statements using Python.
In this article we have discussed about the OS X EI Capitan and how to fix Wi-Fi issue in OS X El Capitan. We have explained how to delete system level preferences and create a new Wi-Fi location to resolve Wi-Fi issue.
Learn the basics of strings in Python: declaration, operations, indices, and slicing. Strings are declared with quotations; for example: s = "string": Strings are immutable.: Strings may be concatenated or multiplied using the addition and multiplic…
Learn the basics of modules and packages in Python. Every Python file is a module, ending in the suffix: .py: Modules are a collection of functions and variables.: Packages are a collection of modules.: Module functions and variables are accessed us…

821 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