I tried to read multiple text files from a local directory into one single pandas dataframe. Since original text files come with extra file extension I renamed it, after all, then I tried to read all text files into single dataframe by read_csv
and concat
from pandas
. Problem is, I am able to read single text files with pandas but when I tried to read a list of text files from a local directory into single dataframe, I got following error:
folder = 'fakeNewsDatasets[Rada]/fakeNewsDataset/fake'
allfiles=os.listdir(folder)
print(allfiles)
['biz01.txt',
'biz02.txt',
'biz03.txt',
'biz04.txt',
'biz05.txt',
'biz06.txt']
then I tried to read those text files into single dataframe as follows:
dfs=pd.concat([pd.read_csv(file, header = None, sep = '\n', skip_blank_lines = True) for file in allfiles], axis=1)
*
FileNotFoundError: [Errno 2] File b'biz02.txt' does not exist: b'biz02.txt' *
I don't understand why this problem occurred because reading a single text file to pandas dataframe works well for me.
df = pd.read_csv('biz01.txt', header = None, sep = '\n', skip_blank_lines = True)
df=df.T
df.columns = ['headline', 'text']
can anyone help me to resolve this issue? how can I fix this error? any better idea?