Colab automatically create directory
Question:
I am trying to implement a code on the CIFAR10 dataset. For each image of the dataset, I am to see if it is correctly classified or not and I want to save two images, for each of the images of the dataset.
One image is the original image and the other is the saliency map of that image. For each image I want the structure of the file saved in my directory to be like this:
'/content/drive/MyDrive/pictures/${name_of_correct_label}/${correct_prediction or not}/${image_index}
So for example, for the index=0 of the dataset, I want to create these two links:
storage_link1='/content/drive/MyDrive/pictures/cat/correct/0/image.jpeg'
storage_link2='/content/drive/MyDrive/pictures/cat/correct/0/saliency.jpeg
‘
The problem is that colab says [Errno 2] No such file or directory: '/content/drive/MyDrive/pictures/cat/correct/0/'
My code is something like this:
original_image = Image.fromarray((test_images[i]*255).astype(np.uint8))
saliency_image = Image.fromarray((absoluteGradientBatch[i]*255).astype(np.uint8))
base_link=storage_link1='/content/drive/MyDrive/pictures/cat/correct/0/'
original_image.save(link+"original.jpeg")
saliency_image.save(link+"saliencyl.jpeg")
Which rises the mentioned error. It is not practical to create all these directories for all indices by hand and I was wondering is there any automated solution for that?
Thanks.
Answers:
Use pathlib
, like this:
from pathlib import Path
Path(baseLink).mkdir(parents=True, exist_ok=True)
And please, snake_case. baseLink -> base_link
, originalImage -> original_image
, etc.
I am trying to implement a code on the CIFAR10 dataset. For each image of the dataset, I am to see if it is correctly classified or not and I want to save two images, for each of the images of the dataset.
One image is the original image and the other is the saliency map of that image. For each image I want the structure of the file saved in my directory to be like this:
'/content/drive/MyDrive/pictures/${name_of_correct_label}/${correct_prediction or not}/${image_index}
So for example, for the index=0 of the dataset, I want to create these two links:
storage_link1='/content/drive/MyDrive/pictures/cat/correct/0/image.jpeg'
storage_link2='/content/drive/MyDrive/pictures/cat/correct/0/saliency.jpeg
‘
The problem is that colab says [Errno 2] No such file or directory: '/content/drive/MyDrive/pictures/cat/correct/0/'
My code is something like this:
original_image = Image.fromarray((test_images[i]*255).astype(np.uint8))
saliency_image = Image.fromarray((absoluteGradientBatch[i]*255).astype(np.uint8))
base_link=storage_link1='/content/drive/MyDrive/pictures/cat/correct/0/'
original_image.save(link+"original.jpeg")
saliency_image.save(link+"saliencyl.jpeg")
Which rises the mentioned error. It is not practical to create all these directories for all indices by hand and I was wondering is there any automated solution for that?
Thanks.
Use pathlib
, like this:
from pathlib import Path
Path(baseLink).mkdir(parents=True, exist_ok=True)
And please, snake_case. baseLink -> base_link
, originalImage -> original_image
, etc.