I would like to add a six-line header to a data file for many files in a loop.
However, I need to add the header to a data properly first...
My inclination (as a python novice) is to write the header and append the data to the header. However, this doesn't work. Can someone recommend a more efficient way to go about adding the header? I am not yet familiar with all the functions and functionality of python, there has to be a better way to do it. Do I have to read the data in line by line to simply attach the header?
Here is my error:
Traceback (most recent call last):
File "C:/work/data/makeheader", line 19, in <module>
AttributeError: 'str' object has no attribute 'append'
I attached the example data file below.
Here is my code:
import os #set working directory workDIR = 'C:\\work\\data' os.chdir(workDIR) runlist=os.listdir(workDIR) data=open('justdata2.txt','a') outfile=open('output.txt','w') row1="ncols 1422" row2="nrows 2044" row3="xllcorner 409924.44886063" row4="yllcorner 3631074.3284728" row5="cellsize 500" row6="NODATA_value -9999" n='\n' header=row1+n+row2+n+row3+n+row4+n+row5+n+row6+n newout=header.append(data) print newout outfile.write(newout)