root@python-10:/home/liujianzuo/python/test# ls
passwd rc.local test1
root@python-10:/home/liujianzuo/python/test# py test1 -r EXIT exit /home/liujianzuo/python/test/rc.local
共修改了0行。
root@python-10:/home/liujianzuo/python/test# py test1 -r exit EXIT /home/liujianzuo/python/test/rc.local the new line is :
第 6 行 : # Make sure that the script will "EXIT 0" on success or any other
the word was replaced is : exit==>EXIT the new line is :
第 16 行 : EXIT 0
the word was replaced is : exit==>EXIT 共修改了2行。
root@python-10:/home/liujianzuo/python/test# cat test1
#coding:utf-8
#!/usr/bin/env python import sys,os,subprocess
#a="liujianzuo"
if len(sys.argv) < 5:
exit("argc less than 4;like python test.py -r 准备替换的字符 要替换的字符 file_path_name") if '-r' in sys.argv:
ar1=sys.argv.index('-r')
ar2=sys.argv[ar1+ 1]
ar3=sys.argv[ar1 + 2]
filname= sys.argv[ar1 + 3]
else:
exit("the first argv must be -r ")
basedir = "%s/%s"%(os.path.dirname(filname),os.path.basename(filname))
f=file('%s'%basedir,"r+")
sum1=0
numli=0
while True:
line=f.readline()
numli+=1
#print line
if ar2 in line:
# print "now pos+++>",f.tell()
f.seek(f.tell()-len(line))
#print f.tell()
new_line=line.replace(ar2,ar3)
f.write(new_line)
print """
\033[31;1mthe new line is :\033[0m
第 %s 行 : %s
the word was replaced is : \033[32;1m%s==>%s\033[0m
""" % (numli,new_line.strip(),ar2,ar3)
sum1+=1
continue
if line == "":
print "file is done bye"
break
elif line == "":
break
print "\033[33;1m 共修改了%s行。\033[0m"%sum1
f.close()

最新文章

  1. play 源码分析
  2. 未能加载文件或程序集“Newtonsoft.Json”或它的某一个依赖项。找到的程序集清单定义与程序集引用不匹配
  3. [Leetcode][JAVA] Insert Interval
  4. shell 运算
  5. 13行代碼開發出来的PHP框架[转]
  6. Windows平台下为Python添加MySQL支持
  7. uva 11151
  8. NetWorker常用术语
  9. ios消息的交互方式
  10. Perfmon 介绍
  11. 打造自己的 JavaScript 武器库(转自SegmentFault公众号)
  12. Linux 系统裁剪笔记 软盘2
  13. 【python标准库模块一】时间模块time学习
  14. [MySQL] explain中的using where和using index
  15. [代码]--给任意网站添加聊天功能,随时聊(fa)天(che)
  16. cliipblard.js 实现点击复制
  17. Java面试题 corejava(一)
  18. 记一次webapi传参数的问题
  19. SharePoint 2013 Troubleshooting——启用 Developer Dashboard
  20. Windows版Jenkins+SVN+Maven自动化部署环境搭建【转】

热门文章

  1. 争夺 &amp; KM思想
  2. sdoi 2009 &amp; 状态压缩
  3. sed 格式化输出df -h
  4. topcoder SRM 624 DIV2 BuildingHeightsEasy
  5. ACM: poj 1094 Sorting It All Out - 拓扑排序
  6. ACM: Just a Hook 解题报告 -线段树
  7. 【JAVA】JMX简单使用方法
  8. libc abi.dylib: terminate_handler unexpectedly threw an exception
  9. IOS 登陆判断问题
  10. java 深入浅出工厂模式