Reputation: 23
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
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
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