import sys
import os
import glob
import string
def isBlankLine(line):
for ch in line:
if ch in [' ',' ',' '] :
continue
else:
return False
return True
sys.argv.append('test*.txt')
sys.argv.append('comment.txt')
if len(sys.argv) < 3:
exit()
filenames = glob.glob(sys.argv[1])
commentfilename = sys.argv[2]
# read the comments
f = file(commentfilename)
commentLines = f.readlines()
commentLines += ' '
f.close()
print string.join(commentLines, '')
# add the comment into each source file
for srcfilename in filenames:
f = file(srcfilename)
srcfileLines = f.readlines()
f.close()
# filter out the previous comment header
for line in srcfileLines:
if isBlankLine(line) :
continue
if line[0] == '#' :
srcfileLines = commentLines + srcfileLines
f = file(srcfilename, 'w')
f.writelines(srcfileLines)
print 'Add comment to '+srcfilename
break
本文介绍了一种使用Python脚本为多个文件批量添加注释的方法。通过读取指定的注释文件并将这些注释插入到一系列文本文件的开头来实现这一目标。此过程会过滤掉原有文件头部可能存在的空白行及注释。
1329

被折叠的 条评论
为什么被折叠?



