2017-10-09 35 views
0

使用腳本時:權限被拒絕試圖複製文件

file = os.path.join(subfolder_name, list_of_files[i]) 
    for dest_folder_finetune, dest_folder_relab in zip(finetune_datasets, relab_datasets): 
     copy(file, dest_folder_finetune) 
     copy(file, dest_folder_relab) 

每20次迭代,我有一個權限被拒絕的問題。堆棧,看起來像:

Traceback (most recent call last): 
    File "/home/revan/boosting_classifier_with_games/dataset_creator.py", line 72, in <module> 
main() 
    File "/home/revan/boosting_classifier_with_games/dataset_creator.py", line 24, in main 
create_test_and_relab(list_of_subfolders) 
    File "/home/revan/boosting_classifier_with_games/dataset_creator.py", line 66, in create_test_and_relab 
copy(file, dest_folder_finetune) 
    File "/home/revan/anaconda2/envs/pytorch/lib/python2.7/shutil.py", line 119, in copy 
copyfile(src, dst) 
    File "/home/revan/anaconda2/envs/pytorch/lib/python2.7/shutil.py", line 83, in copyfile 
with open(dst, 'wb') as fdst: 
IOError: [Errno 13] Permission denied: '/sun_btbivuchmkkzetpo.jpg' 

的「搞笑」的事情是,我絕對有該文件的所有權限。此外,我試圖手動複製它,並且我可以毫無問題地完成它。如果我隨機化進程,同一個文件被複制,但是其他文件(20次迭代之後)不能被複制。 PS:將副本更改爲副本2,給出完全相同的問題。

過去有沒有人經歷過類似的事情?它可能是一個Python或Linux的問題?

+0

你確定你有一個叫做'sun_btbivuchmkkzetpo.jpg'在文件系統中的**根目錄**文件? – jacg

+0

是的。實際上,我通過運行迭代子文件夾的腳本來獲取文件的名稱。然後,我使用該列表中的名稱複製文件,但每20份複印件就會出現此問題。事情是,20這裏沒什麼特別的(子文件夾有100個文件),除了具有讀/寫權限並且能夠手動複製文件之外,我還手動檢查了文件是否存在。 – TheRevanchist

+0

你確實明白,將隨機數據存儲在文件系統的**根目錄中是呃......這是一個相當狡猾和不可取的事情? – jacg

回答

0

其中一個數據集具有錯誤的文件夾名稱,它將目標設置爲文件系統的根目錄。

只得到它的工作,你可以跳過當它試圖複製到根:

import os 
file = os.path.join(subfolder_name, list_of_files[i]) 
for dest_folder_finetune, dest_folder_relab in zip(finetune_datasets, relab_datasets): 
     if os.path.abspath(dest_folder_finetune) != "/": 
      copy(file, dest_folder_finetune) 
     else: 
      print("Warning, path {} for dest_folder_finetune writes to the root of the filesystem".format(dest_folder_finetune)) 
     if os.path.abspath(dest_folder_relab) != "/": 
      copy(file, dest_folder_relab) 
     else: 
      print("Warning, path {} for dest_folder_relab writes to the root of the filesystem".format(dest_folder_relab)) 

但是,如果這是不是活一次腳本的更多,我建議清理和驗證數據集事先代替。

-3

嘗試用「sudo的蟒蛇filename.py」

+0

你應該把你的代碼放在'符號中。 – AreTor

+0

使用'sudo'強迫它而不是理解問題並修復它,似乎不是很好的建議。 – jacg