Details of failed tests for create_train_test_dirs

Below is the error message I saw, any ideas?

Failed test case: There was an error grading this function. Details shown in ‘got’ value below:.
Expected:
no exceptions,
but got:
[Errno 17] File exists: ‘/tmp/tmp9bpz9s0l’.

Hello ,

Thanks for reaching out.We are here to help you!
Can you dm your notebook to me.To solve this error, I need to see your code.

With regards,
Nilosree Sengupta

See below please:

# Define root directory
root_dir = '/tmp/cats-v-dogs'

# Empty directory to prevent FileExistsError is the function is run several times
if os.path.exists(root_dir):
  shutil.rmtree(root_dir)

# 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
  # Don't hardcode the paths. Use os.path.join to append the new directories to the root_path parameter
  os.makedirs(root_path)
  os.makedirs(os.path.join(root_path, 'training'))
  os.makedirs(os.path.join(root_path, 'testing'))
  os.makedirs(os.path.join(root_path, 'training/cats'))
  os.makedirs(os.path.join(root_path, 'testing/cats'))
  os.makedirs(os.path.join(root_path, 'training/dogs'))
  os.makedirs(os.path.join(root_path, 'testing/dogs'))
  pass
  ### END CODE HERE
  
#try:
create_train_test_dirs(root_path=root_dir)
#except FileExistsError:
#print("You should not be seeing this since the upper directory is removed beforehand")

Hey,

I faced the same problem with assignments 1 & 2.

Placing a condition infront helped:

if not os.path.isdir(path):
(indent)os.makedirs(path)

Hope it helps!

Hello @Ran_Duan ,

I have tested this assignment just now with my solution. It’s getting passed with 100/100.

There are definitely always multiple ways to code a particular problem.

To fix this issue, I need to check your full notebook.

Can you please send your notebook as dm- direct message, as we shouldn’t be discussing a full notebook solution openly, as other learners needs to have the scope to try it on their own.

Download your .ipynb file.Click on my name, there is a message option. Then there is an option to upload files.Click on that, and send your notebook, such that I can reciprocate the same on my end and solve it.

With regards,
Nilosree Sengupta

Just sent you the notebook as suggested. Thanks for your time!

Hello @Ran_Duan ,

You’re welcome!
Thanks for sending your file.I have reciprocated the same on my end and fixed the issue now.

In the first graded function, there has been an error as per the test case, though outputs are fine.

It’s mentioned in the hint :

# Don't hardcode the paths. Use os.path.join to append the new directories to the root_path parameter

You had written code for every directory/path individually.Though output comes the expected, but approach was different from what was required as per given in the hint to pass the test case.

You can do the graded function part in lesser lines.Writing logic for one, will do for the rest.

Your file gave the error which you mentioned.Then, in your file only, I had just changed this part and again got full marks in Graded Functions.

To pass the test case, following the instructions of the hint , you can do it like this way :

  1. Take inputs cats and dogs in a list.
  2. Now run a simple loop in the list
  3. Get subdirectories : cats,dogs under directories of training and testing with os.makedirs and append the new directories to the root_path parameter with os.path.join .

Hope this helps.

With regards,
Nilosree Sengupta

3 Likes