使用torch.save时出现RuntimeError: Parent directory C:\Users\86187\Desktop\大学事务\多元数据融合与智能分析\实验二\Amazing-Brick-DFS-and-DRL\DQN_train\..\DQN_train\dqn_weights does not exist.,但是我电脑中这个目录一直存在
时间: 2023-07-01 21:28:15 浏览: 667
这个错误可能是由于你指定的文件夹路径中包含了".."这种相对路径导致的。如果你使用相对路径作为文件夹路径,可能会遇到这种问题,因为程序无法确定相对路径的确切位置。
为了避免这个问题,建议使用绝对路径作为文件夹路径。可以使用 `os.path.abspath()` 函数将相对路径转换为绝对路径,例如:
```
import os
import torch
model = MyModel()
folder_path = os.path.abspath('dqn_weights')
if not os.path.exists(folder_path):
os.makedirs(folder_path)
torch.save(model.state_dict(), os.path.join(folder_path, 'model.pth'))
```
这里使用了 `os.path.abspath()` 将相对路径转换为绝对路径,然后使用 `os.path.join()` 函数将文件名和文件夹路径拼接在一起。同时,使用了 `os.makedirs()` 函数创建了文件夹,如果文件夹不存在的话。
相关问题
super().__init__(torch._C.PyTorchFileWriter(str(name))) RuntimeError: Parent directory C:\Users\86187\Desktop\大学事务\多元数据融合与智能分析\实验二\Amazing-Brick-DFS-and-DRL\DQN_train\..\DQN_train\dqn_weight s does not exist.
这个错误信息的意思是,程序在运行过程中尝试创建一个文件,但是它的父目录不存在。具体来说,错误信息中给出的路径是`C:\Users\86187\Desktop\大学事务\多元数据融合与智能分析\实验二\Amazing-Brick-DFS-and-DRL\DQN_train\..\DQN_train\dqn_weights`,其中`..`表示上一级目录,所以实际路径是`C:\Users\86187\Desktop\大学事务\多元数据融合与智能分析\实验二\Amazing-Brick-DFS-and-DRL\DQN_train\dqn_weights`。这个路径中的`dqn_weights`目录是程序尝试创建的目录,但是它的父目录`DQN_train`并不存在。
要解决这个问题,你需要确保程序运行前`C:\Users\86187\Desktop\大学事务\多元数据融合与智能分析\实验二\Amazing-Brick-DFS-and-DRL\DQN_train`这个目录已经存在。如果这个目录是在程序运行时动态创建的,你需要在程序中加入相应的创建目录的代码。或者,你也可以手动创建这个目录,以解决这个问题。
还是没解决,问题如下:super().__init__(torch._C.PyTorchFileWriter(str(name))) RuntimeError: Parent directory C:\Users\86187\Desktop\大学事务\多元数据融合与智能分析\实验二\Amazing-Brick-DFS-and-DRL\DQN_train\dqn_weights does not exist.
这个错误提示是由于你指定的文件夹路径不存在导致的。你需要确保你的程序中指定的文件夹路径是存在的,如果不存在则需要创建该文件夹。
你可以使用Python的os库来检查文件夹是否存在,并在需要时创建它。例如,下面的代码会检查指定的文件夹是否存在,如果不存在则会创建它:
```python
import os
folder_path = 'C:/Users/86187/Desktop/大学事务/多元数据融合与智能分析/实验二/Amazing-Brick-DFS-and-DRL/DQN_train/dqn_weights'
if not os.path.exists(folder_path):
os.makedirs(folder_path)
```
你可以根据你的实际情况将文件夹路径替换成你程序中出现的路径。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)