Aasim N. Liaqat
Aasim N. Liaqat

Reputation: 23

Reading Specific Files from folder in Python

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.

Upvotes: 1

Views: 1283

Answers (2)

user27640657
user27640657

Reputation: 1

import pandas as pd

data = pd.read_csv('c:/users/path_name/path_name/path_name/filename.csv')

This works. When the path is copied and pasted from explorer need to change the direction of the "/". Usually when copied and pasted, its pasted as "". Changing to / works.

Upvotes: 0

shuvo
shuvo

Reputation: 1956

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

Upvotes: 1

Related Questions