2015-06-22 225 views
1

我試圖編寫這個程序來讀取文件中的目錄並讀取文件的第一行。如果它找到某些關鍵字然後重命名該文件,請複製該文件,並將其更多地複製到相應的目錄中。Errno 13權限被拒絕:?

import os 
import shutil 
import sys 

os.chdir('c:\\source') 

sourcePattern = '2:I103' 
targetDirMt = 'mt' 
targetDirF21 = 'F21' 


curFile = open(os.path.abspath(os.curdir) + '\\' +'ack_nak', 'r+') 

for line in fileinput.input (curFile):  #(os.curdir, 'r+') 
    if sourcePattern in curFile : 
     os.rename(file, '2:I103-'+file) 

    if targetDirMt in curFile : 
     shutil.move(file,'C:\\target\\mt') 

    if targetDirF21 in curFile : 
     shutil.move(file,'C:\\target\\F21') 

爲什麼我得到這個?

Traceback (most recent call last): 
    File "C:\Python34\project1.py", line 12, in <module> 
    curFile = open(os.path.abspath(os.curdir) + '\\' +'ack_nak', 'r+') 
PermissionError: [Errno 13] Permission denied: 'c:\\source\\ack_nak' 
+0

這可能是路徑的問題,嘗試將目錄分隔符更改爲簡單的正斜槓('/')。像:'c:/ source/ack_nak' – Koshinae

+0

我得到相同的錯誤 –

+0

這是ack_nak文件或目錄嗎?你不能「打開」一個目錄。看看'os.walk',這可能會有所幫助。 – Koshinae

回答

-2

嘗試使用超級用戶(sudo)命令。缺乏權限一般可以通過這種方式來彌補。但它會提示您輸入用戶名和密碼,因此您可能需要解決該問題。

IOError: 13, 'Permission denied' when writing to /etc/hosts via Python - 在這裏閱讀答案,他們解釋了所需的代碼。

Open a file as superuser in python - 這是一些更深入的信息。

祝你好運。

+0

從用戶的錯誤消息可以看出,他在Windows上運行,而不是在UNIX/Linux/POSIX上運行。 –

+0

@Rob你看過第二個鏈接和第一個答案嗎?它深入地解釋瞭如何在Windows中執行類似的操作(以管理員身份運行),以解決相同的問題。在你投降之前,請查看鏈接。謝謝。 – Ian

相關問題