0

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'
Sangeun
  • 45
  • 6
  • do pd.read_csv just the filename without extension Missing extension? what happened to fileExtension? – otromas Jun 25 '22 at 20:02
  • @otromas To be honest, I don't know why it uses the filename without extension in pd.read_csv. I just copied the script and tried with it from: https://stackoverflow.com/questions/26415906/read-multiple-txt-files-into-pandas-dataframe-with-filename-as-column-header – Sangeun Jun 26 '22 at 08:05

0 Answers0