0

I want to copy some pictures into different subdirectories 'test' & 'train' located in a directory 'dataset_dogs_vs_cats' that I created using 'makedirs()' librairy. I am facing an issue with the code below related to the access to the subfolders that I created. I do not understand why the permission is denied. I know that there already are several topics discussing it, but after trying different propositions of solutions (closing all files & directories, runing as admin, set path, etc.), my problem is still not solved. The permission Error: [Errno 13] Permission denied:' still remain. Any option please? Thanks.

Here below is the code, taken from this example:

import os
from os import listdir
from os import makedirs

from shutil import copyfile
from random import seed
from random import random

from numpy import asarray
from numpy import save
from numpy import load

from matplotlib import pyplot as plt
from matplotlib.image import imread
%matplotlib inline

##Creation of subdirectories
dataset_regroup = 'dataset_dogs_vs_cats/'
subdirs = ['train/', 'test/']
for subdir in subdirs:
    #creation of labels of subdirectories
    labeldirs = ['dogs/', 'cats/']
    for label in labeldirs:
        newdirectory = dataset_regroup + subdir + label
        makedirs(newdirectory, exist_ok = True)

#Fixing the random number generator at the same seed and test ratio
seed(1)
percentage_test = 0.2

#copy the trainig dataset into the corresponding subdirectory

sub_dataset_regroup = 'C:/Users/Wolf/Documents/LETI-LTM/Codes/CNN/dataset_dogs_vs_cats/train/'

for file2 in os.listdir(sub_dataset_regroup):
    src = sub_dataset_regroup + '/' + file2
    dst_dir = sub_dataset_regroup
    if random() < percentage_test:
        dst_dir = 'test/'
    if file2.startswith('cat'):
        dst = dataset_regroup + dst_dir + 'cats/' + file2
    elif file2.startswith('dog'):
        dst = dataset_regroup + dst_dir + 'dogs/' + file2
        copyfile(src, dst)
Karl
  • 1
  • 1
  • Do you have another service accessing those files or folders? Do the process have access to edit them? Try changing the access level and see if that helps. – Dillon Miller Jun 06 '20 at 23:28
  • Also check that your paths are correct. – Dillon Miller Jun 06 '20 at 23:29
  • Have you checked if you require to run your IDE in administrator mode? – Rajdeep Biswas Jun 07 '20 at 01:58
  • .makedir() has an argument called "mode" which is related to the permissions of the made directories have you played around that? – Anya Samadi Jun 07 '20 at 02:23
  • @DillonMiller, hoping that I understand your remark, I tried to change the path of the directory by moving it into an external hardrive but no improvement. What do you mean by another service please? As in usuaI I only copy and paste the path, so I never had a rpoblem with that, eventough it worth checking! – Karl Jun 07 '20 at 12:24
  • @RajdeepBiswas I did not verify this! I'll let you know. Thanks – Karl Jun 07 '20 at 12:28
  • @AnyaSamadi of course not, but could you be more specific please? – Karl Jun 07 '20 at 12:30
  • .mkdir (): "Creates a directory named path with numeric mode "mode". The default mode is 0777 (octal). On some systems, the mode is ignored. Where it is used, the current mask value is first masked out. Availability: Macintosh, Unix, Windows". I can direct you to this post: https://stackoverflow.com/questions/1627198/python-mkdir-giving-me-wrong-permissions to understand how it works! – Anya Samadi Jun 07 '20 at 16:44

0 Answers0