尝试复制文件时权限被拒绝

时间:2017-10-09 15:49:24

标签: python ubuntu shutil

使用脚本:

    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'

有趣的&#39;事情是我绝对拥有该文件的所有权限。此外,我试图手动复制它,我可以毫无问题地做到这一点。如果我随机化该过程,则会复制相同的文件,但是无法复制其他文件(在20次迭代之后)。

PS:将副本更改为copy2会产生完全相同的问题。

过去有没有人经历过类似的事情?可能是Python还是Linux问题?

2 个答案:

答案 0 :(得分: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 ))

但是,如果这不仅仅是一次活动脚本,我建议事先清理并验证数据集。

答案 1 :(得分:-3)

尝试使用“sudo python filename.py”