Python script for "adding" multiple AAVSO.txt reports to ONE report

Affiliation
American Association of Variable Star Observers (AAVSO)
Thu, 08/29/2013 - 21:26

#!/usr/bin/python
# Easy script v1.0 , Coded by Pierre Hallsten SVO
# Use as you wish!!

import os
import shutil
import fnmatch

'''

This script will read all your AAVSO report textfiles and glue them
together into one file named _mall.txt.
For easy upload to AAVSO

1/ Copy this script and all your report files to whatever dir
2/ Create a subdir namnes res
3/ Copy your header file to res and rename it mall.txt
4/ Run this script from the dir with this script and the AAVSO...text batch

Script will output a new file (_mall.txt) with all your reports glued into one.

You can easily extend or modify script to whatever you need

The header file i use is:

#TYPE=EXTENDED
#OBSCODE=HPIA
#SOFTWARE=VPhot 3.1
#DELIM=,
#DATE=JD
#OBSTYPE=CCD
#NAME,DATE,MAG,MERR,FILT,TRANS,MTYPE,CNAME,CMAG,KNAME,KMAG,AMASS,GROUP,CHART,NOTES

If you have more or fewer lines in your header file you have to change :

offset_end=8 to how many lines your header has -1.

'''
offset_start=1
offset_end=8

#print "Python is really a great language,", "isn't it?";

shutil.copy2('res/mall.txt', 'res/_mall.txt')
dir = os.getcwd()
allfiles = os.listdir(dir)
print allfiles

out = open("res/_mall.txt", "a+") #read and append

for file in os.listdir('.'):
if fnmatch.fnmatch(file, '*.txt'):
fo = open(file, "r",-1)
fo.seek(0)
for x in xrange(offset_start,offset_end): #skip header lines in files
fo.readline()
lines=fo.readlines()
out.writelines(lines)
fo.close()
out.close()

print "done"