Search code examples
pythonarraysfor-loopgaussianpermission-denied

Errno 13 Permission denied: while loading csv files and plotting a gaussian fit


I am getting an error while storing the contents of a csv file into an array. My csv files consists of 2 columns of x-values and y-values. I am using a for loop to loop through a folder of csv files, obtain their file names and store the contents of columns in an array. My first column needs to be stored in xData[] and my second column in yData[]. I am able to print the file names of all the csv files in the folder. However, I am struggling to find the logic in adding the contents of files into an array.

I am getting

Errno 3: Permission denied error.

I also think there is a problem in my while loop.

Once I get the 2 arrays, I want to plot the values of xData against yData and fit a Gaussian into my data.

import csv
import pandas as pd
import numpy as np
from matplotlib import pyplot as plt
from scipy.optimize import curve_fit
from scipy import asarray as ar,exp
import lmfit
from glob import glob 
import os
from lmfit import Parameters, minimize, report_fit, Model 
import glob
path=r'E:\Users\ConfocalUser\Documents\GitHub\qudi'
location=glob.glob(path+'/*.csv')
###check if the path is correct
xData=[]
yData=[]
While location=True:
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

  • It's best to use pandas.read_csv function (https://pandas.pydata.org/pandas-docs/stable/user_guide/io.html#csv-text-files) to get your csv file directly into a dataframe that you can use to plot the data (https://pandas.pydata.org/pandas-docs/stable/getting_started/intro_tutorials/04_plotting.html). Something along the lines of this:

    dataframes = []
    for filepath in glob.iglob(path + "/*.csv"):
        dataframes.append(pd.read_csv(filepath))
    df = pd.concat(dataframes)
    # Let's say your csv looks like this:
    # "a","b"
    # 1,2
    # 2,3
    # 3,4
    # And you want your x data to be the "a" column and y to be "b"
    df.plot.scatter(x="a", y="b")