1

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.

2 Answers 2

1

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
Sign up to request clarification or add additional context in comments.

Comments

0
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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.