How to save a storage file that can be loaded using `torch.Storage.from_file`

I found the code snippet here:

Also, according to the doc, torch.Storage.from_file

If shared is True the file will be created if needed.

Therefore,

To create:

# create.py
import torch

fname = 'tensor.pt'
storage = torch.LongStorage.from_file(fname, True, 40)
torch.LongTensor(storage).copy_(torch.arange(40))

To read:

# read.py
import torch

fname = 'tensor.pt'
storage = torch.LongStorage.from_file(fname, True, 40)
tensor = torch.LongTensor(storage)
print(tensor)
# Demo
$ python create.py
$ python read.py
tensor([ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15, 16, 17,
        18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35,
        36, 37, 38, 39])