0

I am currently working with hundreds of files, all of which I want to read in and view as a numpy array. Right now I am using os.walk to pull all the files from a directory. I have a for loop that goes through the directory and will then create the array, but it is not stored anywhere. Is there a way to create arrays "on the go" or to somehow allocate a certain amount of memory for empty arrays?

2 Answers2

1

Just append them to a list as you go:

lists = []

for dirpath, dirnames, filenames in os.walk(...):
    lists.append(...)
nneonneo
  • 171,345
  • 36
  • 312
  • 383
  • Each file is unique, however; and I don't want to put them all in a single array. –  Apr 10 '13 at 01:29
  • You can put lists inside of lists. So you will end up with a list that contains lists of filenames. – nneonneo Apr 10 '13 at 01:43
0

Python's lists are dynamic, you can change their length on-the-fly, so just store them in a list. Or if you wanted to reference them by name instead of number, use a dictionary, whose size can also change on the fly.

James
  • 2,635
  • 5
  • 23
  • 30