How do I get rid of 'IOError: 13, 'Permission denied'' on Mac OS X

Simmrl picture Simmrl · Jun 21, 2012 · Viewed 25.8k times · Source

Here is my Code that I'm trying to run on Mac OS X:

import getpass #Needed for fetching username
import shutil #Needed for moving Files
import os
var_username = getpass.getuser() #gets username and returns as variable
source_file = r"/Users/%s//Downloads/LogNLock/com.lognlock.loginhook.plist" %(var_username) #the destination of the source file
destination = r"/Library/LaunchAgents" #the target destination for the file to go
shutil.copy(source_file, destination) #moves the source file to the destination folder

And I've googled around and can't figure out why it won't work. Background info: it works when moving files from the desktop to documents for example but I think i need to somehow how root user privileges. im on the administrator account right now.

Answer

aychedee picture aychedee · Jun 21, 2012

The permissions for the folder you are trying to copy the file to are not open enough for you to perform this operation as the user you are are running the script with. This is not really a Python issue. You need to either give the user write permissions to that folder or you need to run the script as root.

To run the script as root:

sudo python your_python_script.py

I believe you'll need to be the Admin user for that to work. 'sudo' is a command that means 'do this as the super user'.

To change the permissions of your folder you could try

sudo chmod a+rw /path/to/folder/that/you/want/to/write/to

Again, you would be doing this as the superuser, chmod is a command to change the permissions of a file or directory. 'a+rw' translates as 'give all users read/write permission. This might be a bad idea... but it sounds like you are just running this locally on your machine.