Torch.save() throws error [Error 2 ] File or Directory not foud

Rashmi_Koparde: <p>when I call torch.save() to save my model , it throws a FileNotFoundError. the trace back is as follow:<br>
<div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/6/b/6b018aeb33d74b6453c394b65018cca8f6722153.png" data-download-href="https://discuss.pytorch.org/uploads/default/6b018aeb33d74b6453c394b65018cca8f6722153" title="image"><img src="https://discuss.pytorch.org/uploads/default/original/3X/6/b/6b018aeb33d74b6453c394b65018cca8f6722153.png" alt="image" data-base62-sha1="fgCoshiACxz3eJPE3IQYHioypB9" width="690" height="194" data-dominant-color="0E0E0E"><div class="meta"><svg class="fa d-icon d-icon-far-image svg-icon" aria-hidden="true"><use href="#far-image"></use></svg><span class="filename">image</span><span class="informations">1078×304 17.3 KB</span><svg class="fa d-icon d-icon-discourse-expand svg-icon" aria-hidden="true"><use href="#discourse-expand"></use></svg></div></a></div></p>
<p>Following is my code.</p>
<pre><code class="lang-auto">        def save_checkpoint(self, ckpt_dir, epoch):
        #self.hparams['dataset_name'] = Iris
        #f'{self.name}_{self.file_name} = iris_model_mlp
        path = os.path.join(
            ckpt_dir, self.hparams['dataset_name'], f'{self.name}_{self.file_name}')

        print(path)
        if not os.path.exists(ckpt_dir):
            os.makedirs(ckpt_dir)

        torch.save({'state': self.net.state_dict(),
                    'epoch': epoch,
                    'loss': self.loss_dict,
                    'optimizer': self.optimizer.state_dict(),
                    'scheduler': self.scheduler.state_dict()}, path , _use_new_zipfile_serialization= False)

        print(f'=&gt; saved the model {self.file_name} to {path}')
        return path
</code></pre>
<p>Any Insights are helpful!!!<br>
Torch version - 1.7.1</p>

ptrblck: <p>I guess the operation is failing, if the intermediate directory wasn’t created.<br>
Based on your code snippet, you are creating <code>ckpt_dir</code>, but never check for <code>self.hparams['dataset_name']</code>.</p>

Rashmi_Koparde: <p><a class="mention" href="/u/ptrblck">@ptrblck</a>   I got the issue. The inner directory was not created so I checked for whether the directory exists or not and if not creating the directory. But now it’s throwing me access denied error as following.<br>
<div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/9/7/97053ac7445af485ec065867ba9bfc91d347d51b.png" data-download-href="https://discuss.pytorch.org/uploads/default/97053ac7445af485ec065867ba9bfc91d347d51b" title="image"><img src="https://discuss.pytorch.org/uploads/default/original/3X/9/7/97053ac7445af485ec065867ba9bfc91d347d51b.png" alt="image" data-base62-sha1="lxZiKsWn5UsKZMnR9wSQopplpsD" width="690" height="344" data-dominant-color="111112"><div class="meta"><svg class="fa d-icon d-icon-far-image svg-icon" aria-hidden="true"><use href="#far-image"></use></svg><span class="filename">image</span><span class="informations">725×362 15.1 KB</span><svg class="fa d-icon d-icon-discourse-expand svg-icon" aria-hidden="true"><use href="#discourse-expand"></use></svg></div></a></div></p>

ptrblck: <p>It seems Windows is disallowing the Python process to create a new directory, so you could either make sure to have the right permissions or create the folder in another directory, which gives you write permissions.</p>