I want to ask how to read multiple text files (all text files including text essays) and save them as each row in a data frame in Python. I got the following code from here StackOverflow but it spits an error message. The error comes from the third step in the code 'frame = pd.read_csv(ijk)'.
Working Code
import pandas as pd import os import glob
# Step 1: get a list of all txt files in the target directory
my_dir = "/content/drive/My Drive/MICUSP_G0"
filelist = []
filesList = []
os.chdir( my_dir )
# Step 2: Build up list of files:
for files in glob.glob("*.txt"):
fileName, fileExtension = os.path.splitext(files)
filelist.append(fileName) #filename without extension
filesList.append(files) #filename with extension
# Step 3: Build up DataFrame:
df = pd.DataFrame()
for ijk in filelist:
frame = pd.read_csv(ijk)
df = df.append(frame)
print (df)
Error message
705 encoding=ioargs.encoding,
706 errors=errors,
--> 707 newline="",
708 )
709 else:
FileNotFoundError: [Errno 2] No such file or directory: 'SOC.G0.02.2_F_NS'