hello …
i got this ,why?
Details of failed tests for create_train_test_dirs
Failed test case: testing/cats subdirectory was not found.
Expected:
True,
but got:
False.
Failed test case: testing/dogs subdirectory was not found.
Expected:
True,
but got:
False.
Failed test case: training/cats subdirectory was not found.
Expected:
True,
but got:
False.
Failed test case: training/dogs subdirectory was not found.
Expected:
True,
but got:
False.
All tests passed for split_data!
All tests passed for train_val_generators!
All tests passed for create_model!
and my function
def create_train_test_dirs(root_path):
START CODE HERE
HINT:
Use os.makedirs to create your directories with intermediate subdirectories
try:
os.mkdir('/tmp/cats-v-dogs')
os.mkdir('/tmp/cats-v-dogs/training')
os.mkdir('/tmp/cats-v-dogs/testing')
os.mkdir('/tmp/cats-v-dogs/training/cats')
os.mkdir('/tmp/cats-v-dogs/training/dogs')
os.mkdir('/tmp/cats-v-dogs/testing/cats')
os.mkdir('/tmp/cats-v-dogs/testing/dogs')
except OSError:
pass
END CODE HERE
1 Like
Hi,
Can you try taking out “/” before tmp in os.mkdir() and see if it works?
Hi @AbdulRahman_Armashi, I think this happened because you hardcoded the paths instead of using the root_path
parameter that the function has and the grader relies on that param to test the function implementation.
try:
os.mkdir(root_path)
os.mkdir(root_path + '/training')
os.mkdir(root_path + '/testing')
os.mkdir(root_path + '/training/cats')
os.mkdir(root_path + '/training/dogs')
os.mkdir(root_path + '/testing/cats')
os.mkdir(root_path + '/testing/dogs')
except OSError:
pass
i tried this and does not work
did not work, i tried alot
[quote=“AbdulRahman_Armashi, post:4, topic:95604, full:true”]
try:
os.mkdir(root_path)
os.mkdir(root_path + '/training')
os.mkdir(root_path + '/testing')
os.mkdir(root_path + '/training/cats')
os.mkdir(root_path + '/training/dogs')
os.mkdir(root_path + '/testing/cats')
os.mkdir(root_path + '/testing/dogs')
except OSError:
pass
I think you are getting pretty close. A couple of improvements I can think of:
- You should avoid using try - except clauses within graded function because they are never required and they will just silence errors, specially never do the except pass thing.
- In this case you do not need to create the
root_path
dir because it already exists (that is why you are getting an OSError)
- Since you are creating nested directories it is better to use
os.makedirs
rather than os.mkdir
as stated in the notebook.
- Although it is a valid approach to concatenate strings to define paths it is a lot better to use
os.path.join
since it will take care of stuff like correctly handling the /
that separate directory names.
Try implementing these changes and let me know if it works!
1 Like
GRADED FUNCTION: create_train_test_dirs
def create_train_test_dirs(root_path):
START CODE HERE
HINT:
Use os.makedirs to create your directories with intermediate subdirectories
os.makedirs(root_path + ‘/training’)
os.makedirs(root_path + ‘/testing’)
os.makedirs(root_path + ‘/training/cats’)
os.makedirs(root_path + ‘/training/dogs’)
os.makedirs(root_path + ‘/testing/cats’)
os.makedirs(root_path + ‘/testing/dogs’)
pass
END CODE HERE
thanks a lot, i tried very much
thanks
can i create sub folders using os.path.join()??
or i need to create folder first then join it ??
thanks a lot
Hello AbdulRahman,
I’m glad to see you here and I see that this function is causing you concern. I suggest you look again at the course notebook (Lab 1) to understand how it works.
1 Like
thank you, i actually returned to lab 1 in course before ask here , but in this lab the directory already created and we only join it.
Hello AbdulRahman,
The notebook (Lab 1) is indeed the example that is studied during the course, it allows you to understand how to create the directories as you’re asked during the programming assignment.
1 Like
Did your issue got resolved? I am getting same error.
are you going to solve the problem or just tell us to do what we already do?
def create_train_test_dirs(root_path):
training_dir = "training"
testing_dir = "testing"
cats_dir = "cats"
dogs_dir = "dogs"
training_dir_path = os.path.join(root_path, training_dir)
testing_dir_path = os.path.join(root_path, testing_dir)
training_cats_dir_path = os.path.join(root_path, training_dir, cats_dir)
training_dogs_dir_path = os.path.join(root_path, training_dir, dogs_dir)
testing_cats_dir_path = os.path.join(root_path, testing_dir, cats_dir)
testing_dogs_dir_path = os.path.join(root_path, testing_dir, dogs_dir)
os.makedirs(training_dir_path)
os.makedirs(testing_dir_path)
os.makedirs(training_cats_dir_path)
os.makedirs(training_dogs_dir_path)
os.makedirs(testing_cats_dir_path)
os.makedirs(testing_dogs_dir_path)
2 Likes
When you use os.makedirs(), you don’t have to put the training and testing dirs. Because makedirs works recursively and will make them automatically when you make interior directories. So you can just remove them.
2 Likes
Hello rachimvdr,
I’m happy that you’re following this MOOC and that you’ve paid attention to the labs made available. Giving the answer to the exercises directly is against Coursera’s code of honor, but look at lab 1 and you’ll see that the part of code mentioned above doesn’t quite match because it’s missing steps. Once you understand lab 1, finding the missing part on your own will be a source of pride and better understanding for you.
1 Like
hello,
I used the same code but I am getting Name error name os not defined!!!
my error shows this
6 # Empty directory to prevent FileExistsError is the function is run several times
----> 7 if os.path.exists(root_dir):
8 shutil.rmtree(root_dir)
9
NameError: name ‘os’ is not defined