def replace(file_path, old_pname, new_pname):
'''
# 函数作用:替换指定文件中特定字符串
'''
try:
f = open(file_path,'r+')
all_lines = f.readlines()
f.seek(0)
f.truncate()
for line in all_lines:
line = line.replace(old_pname, new_pname)
f.write(line)
# print "--replace "+ old_pname+" to "+new_pname
# print line
f.close()
except Exception,e:
print e
# 拷贝文件
def copyFiles(sourceDir, targetDir):
# print sourceDir
for f in os.listdir(sourceDir):
sourceF = os.path.join(sourceDir,f)
targetF = os.path.join(targetDir,f)
if os.path.isfile(sourceF):
#创建目录
if not os.path.exists(targetDir):
os.makedirs(targetDir)
#文件不存在,或者存在但大小不同,覆盖
if not os.path.exists(targetF) or (os.path.exists(targetF) and (os.path.getsize(targetF) != os.path.getsize(sourceF))):
open(targetF,"wb").write(open(sourceF,"rb").read())
print u"%s...复制完毕......" % targetF
else:
print u"%s...已存在,不重复复制....." % targetF
#如目标文件是文件夹调用本身
if os.path.isdir(sourceF):
copyFiles(sourceF,targetF)