简体   繁体   中英

Python Error:OSError: [Errno 22] Invalid argument

Traceback (most recent call last):
File "main.py", line 312, in <module> display=viz) 
File "C:\Users\yang6\PycharmProjects\DeepHyperX\models.py", line 1059, in train save_model(net, camel_to_snake(str(net.__class__.__name__)), data_loader.dataset.name, epoch=e, metric=abs(metric)) 
File "C:\Users\yang6\PycharmProjects\DeepHyperX\models.py", line 1068, in save_model torch.save(model.state_dict(), model_dir + filename + '.pth') 
File "C:\Users\yang6\Anaconda3\envs\DeepHyperX\lib\site-packages\torch\serialization.py", line 260, in save return _with_file_like(f, "wb", lambda f: _save(obj, f, pickle_module, pickle_protocol)) 
File "C:\Users\yang6\Anaconda3\envs\DeepHyperX\lib\site-packages\torch\serialization.py", line 183, in _with_file_like f = open(f, mode) 
OSError: [Errno 22] Invalid argument: './checkpoints/hamida_et_al/PaviaU/2020-07-03 10:52:46.094626_epoch2_0.78.pth'

My file path is './checkpoints/hamida_et_al/PaviaU/2020-07-03 10:52:46.094626_epoch2_0.78.pth' , so how to settle it?

enter image description here

My Code:

def save_model(model, model_name, dataset_name, **kwargs):
 model_dir = 'C:/Users/yang6/PycharmProjects/DeepHyperX/checkpoints/' + model_name + "/" + dataset_name + "/"
 if not os.path.isdir(model_dir):
     os.makedirs(model_dir, exist_ok=True)
 if isinstance(model, torch.nn.Module):
     filename = str(datetime.datetime.now()) + "_epoch{epoch}_{metric:.2f}".format(**kwargs)
     tqdm.write("Saving neural network weights in {}".format(filename))
     torch.save(model.state_dict(), model_dir + filename + '.pth')
 else:
     filename = str(datetime.datetime.now())
     tqdm.write("Saving model params in {}".format(filename))
     joblib.dump(model, model_dir + filename + '.pkl')

Since your are on Windows and working with PyCharm, it seems to me you need to pass the absolute path to open() rather than the relative path see here .

Further make sure you use the correct escape sequences if there is a blank space in four path, see here .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM