So, I have a bunch of python files (hundreds actually) that need a comment header at the top that contains the product name, a license reference notice, copyright information and other things. What is the best way to do this in a batch-like way? In other words, is there a tool I can use to specify what the header will be and what directory to apply this header to along with a *.py filter or something along those lines? By the way, all of the header info is identical for every file.
If instead of the batch approach you would rather use python itself, a very simplified version could be written as:
import os, sys
def main():
HEADER = '''# Author: Rob
# Company: MadeupOne
# Copyright Owner: Rob
'''
filelist = []
for path, dir, files in os.walk(sys.argv[1]):
for file in files:
if file.endswith('.py'):
filelist.append(path + os.sep + file)
for filename in filelist:
try:
inbuffer = open(filename, 'U').readlines()
outbuffer = [HEADER] + inbuffer
open(filename, 'wb').writelines(outbuffer)
except IOError:
print 'Please check the files, there was an error when trying to open %s...' % filename
except:
print 'Unexpected error ocurred while processing files...'
if __name__ == '__main__': main()
Just pass the directory containing the files you want to alter and it will recursively prepend HEADER to all .py files on the path.