import os
导入书
#-* -编码: CP 936-* -
导入代码
from sys import argv
defreplace_all_files(path ) :
cnt=0
for root,dirs,filesinOS.walk(path ) :
for name in files:
ifname.Endswith(.CPP ) ) :
f=codecs.open(OS.path.join ) root,name ),' rb ' ) )
text=f.read ()
text1=text
iftext.find(STDafx.h () ) 0:
key='#include 'stdafx.h'rn '
if text [ :3 ]==codecs.BOM _ utf 83360
text1=' % s % s % s ' % (codecs.BOM _ utf8,key,text[3:] () ) ) ) ) ) )。
else:
text1='%s%s' % (key,text )
f.seek(0) )。
f.truncate () )
f.write(text1)。
cnt=cnt 1
print ' [ % d ] relpace file : ' % CNT OS.path.join (root,name ) ) ) ) ) ) ) )。
f.close () )
if __name__=='__main__':
try:
replace_all_files(argv[1] )
except Exception,e:
print e.message