This topic has been covered many times here but none of the solutions have worked for me. I am trying to add all text files from a folder into a dataframe. The code below is working IF I only have 1 file in the folder but as soon as I add another, I get the error: ParserError: Error tokenizing data. C error: Expected 1 fields in line 40, saw 2.
import pandas as pd
import os
import glob
#define path to dir containing the summary text files
files_folder = "/data/TB/WA_dirty_prep_reports/"
files = []
#create a df list using list comprehension
files = [pd.read_csv(file) for file in glob.glob(os.path.join(files_folder,"*txt"))]
#concatanate the list of df's into one df
files_df = pd.concat(files)
print(files_df)
I have also tried this approach with the same result:
import glob
import pandas as pd
path = '/data/TB/WA_dirty_prep_reports'
summary_files = glob.glob(path + "/*.txt")
df_list = []
df_list = (pd.read_csv(file) for file in summary_files)
big_df = pd.concat(df_list, ignore_index=True)