title: Add Header row to multiple .txt files using
body: <p>Python newbie hoping I can get a little bit of help. I have a bunch of .txt files listing GPS data. I would like to create a python script that will open each .txt file in the directory and add headers (prior to converting to .csv and then processing as ESRI GIS feature classes). I have some python code that will list the .txt files in my target directory and I also have some python code that will add the headers to a single specified .txt file but I am not sure how to tie the two bits of code together so the whole script will process all the .txt files</p>
  <p><strong>Python Code to list .txt files:</strong></p>
  <pre><code>import glob
  
  Workspace = &quot;C:\\Pathway\\ToTarget&quot;
  date = time.strftime('%y_%m_%d')
  Directory = Workspace + &quot;\\&quot; + date
  
  glob.glob(Directory + &quot;./*.txt&quot;)
  </code></pre>
  <p>So, if i do</p>
  <pre><code>import glob
  
  Workspace = &quot;C:\\Pathway\\ToTarget&quot;
  date = time.strftime('%y_%m_%d')
  Directory = Workspace + &quot;\\&quot; + date
  
  listoffiles = glob.glob(Directory + &quot;./*.txt&quot;)
  print listoffiles
  </code></pre>
  <p>I get a list of the .txt files in my target directory. So far all good.</p>
  <p>================</p>
  <p><strong>Python to add header to a specified .txt file</strong></p>
  <pre><code>listoffiles = &quot;C:\\Pathway\\to\\Target_Dir\\Log - Copy (2).txt&quot;
  headers = &quot;SENT_ID,TIME_UTC,NAV_WARN,LAT,N_S,LONG,E_W,SPEED,COURSE,DATE,MAG_VAR,,CHECK&quot;.split()
  
  for line in fileinput.input([listoffiles], inplace=True):
      if fileinput.isfirstline():
          print '\t'.join(headers)
      print line,
  </code></pre>
  <p>This changes the first line of the specfied .txt file. All Good.</p>
  <p>================</p>
  <p><strong>Amalgamated Code</strong></p>
  <p>However if i do this:</p>
  <pre><code>import glob
  import fileinput
  import os
  import time   
  
  Workspace = &quot;C:\\Pathway\\ToTarget&quot;
  date = time.strftime('%y_%m_%d')
  Directory = Workspace + &quot;\\&quot; + date
  
  listoffiles = glob.glob(Directory + &quot;./*.txt&quot;)
  headers = &quot;SENT_ID,TIME_UTC,NAV_WARN,LAT,N_S,LONG,E_W,SPEED,COURSE,DATE,MAG_VAR,,CHECK&quot;.split()
  
  for line in fileinput.input([listoffiles], inplace=True):
      if fileinput.isfirstline():
          print '\t'.join(headers)
      print line,
  </code></pre>
  <p>The error that I get is: TypeError: can only concatenate list (not &quot;str&quot;) to list</p>
  <pre><code>  Message   File Name   Line    Position    
  Traceback               
  &lt;module&gt;    C:\Users\pennychr\Desktop\GPS_2.py  58      
  next    C:\Python27\ArcGIS10.2\lib\fileinput.py 253     
  readline    C:\Python27\ArcGIS10.2\lib\fileinput.py 318     
  &quot;TypeError: can only concatenate list (not &quot;&quot;str&quot;&quot;) to list&quot;
  </code></pre>
  <p>If anyone can give me some pointers as to the correct way to help me work around this issue, that would be great.</p>