1

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.

4

4 回答 4

5

Bash batch syntax:

for i in `find {DIRECTORY} -name "*.py"`; do
    cat - $i > /tmp/f.py <<EOF
{HEADER_BLOCK}
EOF
    mv /tmp/f.py $i
done
于 2012-12-10T00:22:05.220 回答
1

You can actually use python itself to follow a pythonic way.

To prepend or append some text to a file, use:

with open('filename.py','rb') as f:
    text = f.read()
    text = prependText + text
    text = text + postText
    // whatever you want to manipulate with the code text
with open('filename.py','wb') as f:
    f.write(text)

Since python modules usually demonstrate a tree structure, you can always use walk function (os.path.walk) to navigate to any level of depth and apply any custom logic according to path and/or filename.

于 2012-12-10T01:20:09.897 回答
1

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.

于 2012-12-10T01:20:28.243 回答
0

Updated above script to work with python 3 and ignore hidden folders

import os, sys

def main(path):
    HEADER = '''#!/usr/bin/python3

# Copyright : 2021 European Commission
# License   : 3-Clause BSD


'''

    filelist = []
    for path, dir, files in os.walk(sys.argv[1]):
        if '/.' not in path:
            for file in files:
                if file.endswith('.py'):
                    filelist.append(path + os.sep + file)
        
    for filename in filelist:
        try:
            inbuffer = open(filename, 'r').readlines()
            outbuffer = [HEADER] + inbuffer

            open(filename, 'w').writelines(outbuffer)
            print(f"Header is added to the file: '{filename}'.")
        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()
于 2021-01-15T15:00:03.460 回答