简体   繁体   中英

How to use np.save to save files in different directory in python?

I want to save training in a different folder named Check . How can I save this using np.save command? I read about np.save command from documentation but it doesn't describe how to save it in different directory.

sample = np.arange(100).reshape(10,10)
split = 0.7
index = int(floor(len(sample)*split))
training = sample[:index]
np.save("Check"+'train_set.npy',training)

From the ( DOCS ):

file : file, str, or pathlib.Path

File or filename to which the data is saved. If file is a file-object, then the filename is unchanged. If file is a string or Path, a .npy extension will be appended to the file name if it does not already have one.

This states that if the filename has a directory (ie: Path), it will be stored there. So something like this should do what you need:

import os
np.save(os.path.join('Check', 'train_set'), training)

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