-1

I use numpy.savez to register my arrays as .npz files ( and it works very well).

But it saves the files in the folder where there is the script. Technically, my program is a while loop which will save 100 files one at a time.

I would like to be able to select at the beginning of the process ( or write full adress) a place on my computer ,where to save, and then for each iteration have numpy.savez to save them where I indicated.

Because I would not want either to say 100 times " I want you to save it there".

I looked at the scipy docs but I did not find any solutions.

Thanks for helping me !

Magea
  • 137
  • 1
  • 10
  • 1
    Er.. shouldn't **you** pass a path to the directory you want to save the files no? – EdChum Apr 15 '15 at 12:25
  • I understand the words you use but not the meaning ! ahah Sorry my "noobish" approach of it. What you say seems to be what I am looking for, but what seems obvious to you is not to me ^^ Can you elaborate ? – Magea Apr 15 '15 at 12:27
  • [Read the docs!](http://docs.scipy.org/doc/numpy/reference/generated/numpy.savez.html) The first parameter is a file or filename. – jonrsharpe Apr 15 '15 at 12:27
  • Right now I use the first parameter to define my file name. You say to manage where it will save, I should write the full adress of the location I want it to go ? – Magea Apr 15 '15 at 12:28
  • Yes, precisely. If you just give the name of the file, rather than the full path, it will go into the current working directory (although note [you *can* change the cwd](http://stackoverflow.com/q/1810743/3001761)). – jonrsharpe Apr 15 '15 at 12:30
  • I do not understand : open file (file-like object) actually – Magea Apr 15 '15 at 12:31
  • 1
    That's so if you've *already* opened a file (`with open(filename) as f_:`) you can pass the open file object (`f_`) rather than the name/path (`filename`). – jonrsharpe Apr 15 '15 at 12:32

1 Answers1

3

Have the path the the folder that you want to save them in as a variable, then combine that path with the filename of each with os.path.join

import os
import numpy as np

BASE_PATH = "/path/to/base"

for i in xrange(100):
    x = np.random.normal(0.0, 1.0, 100) #make the array to save
    file_name = "{0}.npz".format(i)
    np.savez( os.path.join(BASE_PATH, file_name), x )
Simon Gibbons
  • 6,969
  • 1
  • 21
  • 34