Chinaunix首页 | 论坛 | 博客
  • 博客访问: 105290
  • 博文数量: 23
  • 博客积分: 1410
  • 博客等级: 上尉
  • 技术积分: 235
  • 用 户 组: 普通用户
  • 注册时间: 2007-04-19 22:44
文章分类

全部博文(23)

文章存档

2011年(1)

2008年(1)

2007年(21)

我的朋友
最近访客

分类: Python/Ruby

2007-04-23 17:15:15


#初学python:
#1.
##############################

from os.path import walk, join, normpath
import sys
 
if len(sys.argv) != 2:
    print 'input param error, like: add_str filepath'
    sys.exit(0)
 
debug_h='#include \"/dvr/debug/my_debug.h\"\n'
 
def add_str(file_c):
    f = file(file_c)
    all = f.readlines()
    f.close()
 
    print file_c, '---starting replace...'
    all.insert(0,debug_h)
    ff = file(file_c, 'w')
    for i in range(0, len(all)):
        ff.write(all[i])
    ff.close()
    print file_c, '---replace over...'
    
 
def visit(arg, dirname, names):
    files=[normpath(join(dirname, file)) for file in names]
 
    for i in range(0, len(files)):
        if files[i][-2:] in ['.h'] :
            add_str(files[i])
    
if __name__=="__main__":
    path=sys.argv[1]
    print 'Starting now...',path
    walk(path, visit, 0)
    print 'Completed....................'

###############################
#2.

from os.path import walk, join, normpath
import sys
 
if len(sys.argv) != 4:
    print 'input param error, like: replace filepath oldstr newstr'
    sys.exit(0)
 
old_str = sys.argv[2]
new_str = sys.argv[3]
 
def replace_c(file_c, old, new):
    f = file(file_c)
    all = f.readlines()
    f.close()
 
    print file_c, '---starting replace...'
    for i in range(0, len(all)):
        all[i] = all[i].replace(old, new)
 
    ff = file(file_c, 'w')
    for i in range(0, len(all)):
        ff.write(all[i])
    ff.close()
    print file_c, '---replace over...'
    
 
def visit(arg, dirname, names):
    files=[normpath(join(dirname, file)) for file in names]
 
    for i in range(0, len(files)):
        if files[i][-2:] in ['.c', '.h'] :
            replace_c(files[i], old_str, new_str)
        if files[i][-4:] in ['.cpp', '.txt'] :
            replace_c(files[i], old_str, new_str)
    
if __name__=="__main__":
    path=sys.argv[1]
    print 'Starting now...',path
    walk(path, visit, 0)
    print 'Completed....................'

阅读(1822) | 评论(0) | 转发(0) |
0

上一篇:转载-Debian 入门 工具

下一篇:升级vim7.0

给主人留下些什么吧!~~