Home > other >  Reading Specific Files from folder in Python
Reading Specific Files from folder in Python

Time:03-17

I have folder with 12000 csv sample files and I need to read only certain files of interest from it. I have a list with filenames that I want to read from that folder. Here's my code so far

Filenames # list that contains name of filenames that I want to read 

# Import data
data_path= "/MyDataPath"
data = []

i=0
# Import csv files
#I feel I am doing a mistake here with looping Filenames[i]
for file in glob.glob(f"{data_path}/{Filenames[i]}.csv", recursive=False): 

    df = pd.read_csv(file,header=None)

    # Append dataframe
    data.append(df)
    i=i 1

This code only reads first file and ignores all other.

CodePudding user response:

The problem is you are not iterating over the Filenames.

Try the following:

# i=0
# Import csv files
#I feel I am doing a mistake here with looping Filenames[i]
for f in Filenames: 
    file = glob.glob(f"{data_path}/{f}.csv", recursive=False)
    df = pd.read_csv(file,header=None)
    # Append dataframe
    data.append(df)
    # i=i 1
  • Related