Search code examples
pythonarrayscsvglob

Unable to read filenames in a for loop and print the arrays


I am trying to loop through a folder of 70 csv files. Each file has 2 columns and I want to store them in 2 different arrays. I am not able to print the file names in the folder and my arrays are not being appended. I want the output to produce the file name with its a respective arrays.

path=r"E:\Users\ConfocalUser\Documents\GitHub\qudi"
files=glob.glob(os.path.join(path,'*.csv')

xData=[]
yData=[]
for file in files:
    print(file)
    with open(path,"r") as f_in:
        reader=csv.reader(f_in)
        next(reader)
        for line in reader:
            try:
                float_1,float_2=float(line[0]),float(line[1])
                xData.append(float_1)
                yData.append(float_2)
            except ValueError:
                continue

Solution

  • Why do you do

        with open(path,"r") as f_in:
            reader=csv.reader(f_in)
            next(reader)
    

    instead of

        with open(file,"r") as f_in:
            reader=csv.reader(f_in)
    

    reader() expects a csv file, not a list of pathnames.