0

i want to load all .csv files from a folder into a list of seperate dataframes for each file

the folder is called coins.

for file in './coins': 
    logs_total = [pd.read_csv('./coins/'+file, engine='python')] 

The error:

IsADirectoryError: [Errno 21] Is a directory: './coins/.'

without engine='python' its :

ParserError: Error tokenizing data. C error: Calling read(nbytes) on source failed. Try engine='python'.

1 Answer 1

3

Your for loop does not reference the files in the coins folder. All Python knows is that './coins' is a string, and you are iterating over each letter in that string.

Also, if you want to build a list of data frames with a for loop, you should create the list outside of the loop first and append to it (or you can use a list comprehension).

To access the files, you can import the either os or glob to get the file names. Here is an example using os.

import os
import pandas as pd

log_total = []
for file in os.listdir('./coins'):
    log_total.append(pd.read_csv('./coins/'+file))

Here is an example using glob and a list comprehension.

from glob import glob
import pandas as pd

log_total = [pd.read_csv(f) for f in glob('./coins/*.csv')]
Sign up to request clarification or add additional context in comments.

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.