2012-06-20 52 views
4

這裏是我的,我試圖在Mac OS X上運行代碼:如何擺脫「IO錯誤:13,‘權限被拒絕’」在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 

而且我已經搜索一下,無法弄清楚爲什麼它不起作用。 背景信息:將文件從桌面移動到文檔時有效,但我認爲我需要以某種方式根用戶權限。即時在管理員帳戶。

+0

source_file = r「/ Users /%s // ...」< - 2 // // 此外,字符串插值應該只是%var_username或%(var_username,) –

回答

9

您試圖將文件複製到的文件夾的權限不足以讓您以您正在運行該腳本的用戶的身份執行此操作。這不是一個真正的Python問題。您需要爲用戶提供對該文件夾的寫入權限,或者需要以root身份運行腳本。

以root身份運行該腳本:

sudo python your_python_script.py 

我認爲你需要成爲管理員用戶對於工作。 'sudo'是一個意味着'以超級用戶身份進行操作'的命令。

要更改文件夾的權限,你可以嘗試

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

同樣,你會做這樣的超級用戶,文件模式是改變文件或目錄的權限的命令。 'a + rw'翻譯爲'給所有用戶讀/寫權限。這可能是一個壞主意......但聽起來你只是在你的機器上本地運行它。

+0

我該怎麼做事情呢?我很新。 – Simmrl

相關問題