天天看點

批量增加檔案頭注釋的python小程式

批量增加檔案頭注釋的python小程式

import  sys

批量增加檔案頭注釋的python小程式

import  os

批量增加檔案頭注釋的python小程式

import  glob

批量增加檔案頭注釋的python小程式

import  string

批量增加檔案頭注釋的python小程式
批量增加檔案頭注釋的python小程式

def  isBlankLine(line):

批量增加檔案頭注釋的python小程式

     for  ch  in  line:

批量增加檔案頭注釋的python小程式

         if  ch  in  [ '   ' , ' ' , ' ' ] :

批量增加檔案頭注釋的python小程式

             continue

批量增加檔案頭注釋的python小程式

         else :

批量增加檔案頭注釋的python小程式

             return  False

批量增加檔案頭注釋的python小程式

     return  True

批量增加檔案頭注釋的python小程式
批量增加檔案頭注釋的python小程式

sys.argv.append( ' test*.txt ' )

批量增加檔案頭注釋的python小程式

sys.argv.append( ' comment.txt ' )

批量增加檔案頭注釋的python小程式

if  len(sys.argv)  <   3 :

批量增加檔案頭注釋的python小程式

    exit()

批量增加檔案頭注釋的python小程式
批量增加檔案頭注釋的python小程式

filenames  =  glob.glob(sys.argv[ 1 ])

批量增加檔案頭注釋的python小程式

commentfilename  =  sys.argv[ 2 ]

批量增加檔案頭注釋的python小程式
批量增加檔案頭注釋的python小程式

#  read the comments

批量增加檔案頭注釋的python小程式

f  =  file(commentfilename)

批量增加檔案頭注釋的python小程式

commentLines  =  f.readlines()

批量增加檔案頭注釋的python小程式

commentLines  +=   ' '

批量增加檔案頭注釋的python小程式

f.close()

批量增加檔案頭注釋的python小程式

print  string.join(commentLines,  '' )

批量增加檔案頭注釋的python小程式
批量增加檔案頭注釋的python小程式

#  add the comment into each source file

批量增加檔案頭注釋的python小程式

for  srcfilename  in  filenames:

批量增加檔案頭注釋的python小程式

    f  =  file(srcfilename)

批量增加檔案頭注釋的python小程式

    srcfileLines  =  f.readlines()

批量增加檔案頭注釋的python小程式

    f.close()

批量增加檔案頭注釋的python小程式

     #  filter out the previous comment header

批量增加檔案頭注釋的python小程式

     for  line  in  srcfileLines:

批量增加檔案頭注釋的python小程式

         if  isBlankLine(line) :

批量增加檔案頭注釋的python小程式

             continue

批量增加檔案頭注釋的python小程式

         if  line[0]  ==   ' # '  :

批量增加檔案頭注釋的python小程式

            srcfileLines  =  commentLines  +  srcfileLines

批量增加檔案頭注釋的python小程式

            f  =  file(srcfilename,  ' w ' )

批量增加檔案頭注釋的python小程式

            f.writelines(srcfileLines)

批量增加檔案頭注釋的python小程式

             print   ' Add comment to  ' + srcfilename

批量增加檔案頭注釋的python小程式

         break

批量增加檔案頭注釋的python小程式