title: How to read multiple files with Python and combine into single dataframe with identifier for each file?
body: <p>I would like to read in multiple files from a directory, add an identifier for each file, and concatenate them into a single dataframe. Below is an example with two files but I am looking for a more efficient way to generalize the procedure to many files. I'd like to add an id column that identifies each file (so my question slightly differs from <a href="https://stackoverflow.com/questions/20906474/import-multiple-csv-files-into-pandas-and-concatenate-into-one-dataframe">this</a>)</p>
  <p>Reprex:</p>
  <pre><code># file1.csv
  # file2.csv
  
  import pandas as pd
  
  df1 = pd.read_csv(&quot;file1.csv&quot;)
  df[&quot;id&quot;] = &quot;file1&quot;
  
  df2 = pd.read_csv(&quot;file2.csv&quot;)
  df2[&quot;id&quot;] = &quot;file2&quot;
  
  
  df_all = pd.concat[df, df2]
  
  </code></pre>
  <p>I would also like to use it for .txt files like so:</p>
  <pre><code>&gt;&gt;&gt; %cat 'file1.txt'
  A|B|C
  1|0.5|good
  2|0.2|bad
  3|0.6|good
  4|0.4|ok
  
  &gt;&gt;&gt; %cat 'file2.txt'
  A|B|C
  1|0.2|bad
  2|0.5|ok
  3|0.9|good
  4|0.3|ok
  5|0.7|bad
  </code></pre>