Save_image in a new folder

Hello!

How can I save images (from a VAE, but it doesn’t matter) in a new folder? What I want is to automatically create a new folder if it doesn’t exist, and save there the pictures. And if it exists, just to use it.

I’m using this but it doesn’t work:

torchvision.utils.save_image(code, f"/ImageOutput/code{epoch}.png", nrow=num_rows)   

The point is that I would like to create or use the folder named ImageOutput.

Thanks!

This might not be the most elegant way but it definitely works

if not(os.path.isdir('/ImageOutput')):
        os.mkdir('/ImageOutput')
torchvision.utils.save_image(code, f"/ImageOutput/code{epoch}.png", nrow=num_rows)