Attempting to read data from multiple files to multiple arrays
Question: I would like to be able to read data from multiple files in one folder to
multiple arrays and then perform analysis on these arrays such as plot graphs
etc. I am currently having trouble reading the data from these files into
multiple arrays.

My solution process so far is as follows;

    
    
    import numpy as np
    import os 
    
    #Create an empty list to read filenames to 
    
    filenames = []
    
    for file in os.listdir('C\\folderwherefileslive'):
         filenames.append(file)
    

This works so far, what I'd like to do next is to iterate over the filenames
in the list using numpy.genfromtxt.

I'm trying to use os.path join to put the individual list entry at the end of
the path specified in listdir earlier. This is some example code:

    
    
    for i in filenames:
        file_name = os.path.join('C:\\entryfromabove','i')
        'data_'+[i] = np.genfromtxt('file_name',skiprows=2,delimiter=',')
    

This piece of code returns "Invalid syntax".

To sum up the solution process I'm trying to use so far: 1\. Use os.listdir to
get all the filenames in the folder I'm looking at. 2\. Use os.path.join to
direct np.genfromtxt to open and read data from each file to a numpy array
named after that file.

I'm not experienced with python by any means - any tips or questions on what
I'm trying to achieve are welcome.


Answer: For this kind of task you'd want to use a dictionary.

    
    
    data = {}
    for file in os.listdir('C\\folderwherefileslive'):
        filenames.append(file)
        path = os.path.join('C:\\folderwherefileslive', i)
        data[file] = np.genfromtxt(path, skiprows=2, delimiter=',')
    
    # now you could for example access
    data['foo.txt']
    

* * *

Notice, that everything you put within single or double quotes ends up being a
character string, so `'file_name'` will just be some characters, whereas using
`file_name` would use the value stored in variable by that name.

