0

Lets say I have a directory full of text files:

array1.txt
array2.txt
array3.txt

If I were to import a single text file using numpy, I'd do something like this:

array1 = numpy.loadtxt('filepath/array1.txt')

Notice that the numpy array has the same name as the text file it was created with (aside from .txt extension).

I'm not sure how to do someting like this in a loop though. Essentially, I want to look in the directory 'filepath', find every text file of the form name.txt and load it as a numpy array which is named based on the name of the text file name.

Thanks

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
Ian Gullett
  • 107
  • 9
  • Creating a large number of variables like that is not a good idea in python. Dictionaries are a better way of associating a name with a value. Or collect both names and arrays in list. – hpaulj Mar 01 '20 at 16:34
  • See this [answer](https://stackoverflow.com/a/3964696/6091318), but additionally use `os.path.join` to construct file full path – Brenlla Mar 01 '20 at 16:35

0 Answers0