Read through all .txt files in a directory and load to Pandas dataframe

In [15]:
# Assign directory
import os
import pandas as pd

##os.path.expanduser used here in place of /Users/YOURUSERNAME/Desktop
indir  = os.path.expanduser("~/desktop/python/misc/files/")

df = pd.DataFrame()
for root, dirs, filenames in os.walk(indir):
    for f in filenames:
        fullpath = os.path.join(indir, f)
        frame = pd.read_csv(fullpath, sep='\t')
        df = df.append(frame)
        print(f)
        
testfile1.txt
testfile2.txt
testfile3.txt
In [16]:
df.head()
Out[16]:
Player Salary team
0 Hamilton 500000 Reds
1 Happ 100000 Cubs
0 Duvall 200000 Reds
1 Bryant 900000 Cubs
0 Votto 500000 Reds
In [ ]: