需求:

  编写sed命令脚本

代码如下

 # Author:Lee Sir

 import sys,os

 des_file = r'E:\StartPython\day3\test.txt'
des_file1 = r'E:\StartPython\day3\test1.txt' parameter = ['','Somehow','',des_file] def usage(parameter):
if len(parameter) == 4:
if isinstance(parameter[1],str) and isinstance(parameter[2],str):
old_str = parameter[1]
new_str = parameter[2]
if os.path.exists(parameter[3]):
des_file = parameter[3]
return True, old_str, new_str, des_file
return False def check_string_exist(old,file):
with open(file,encoding='utf-8') as fd:
for line in fd:
if old not in line:
return False
else:
return True def replace(old,new,file):
with open(file,'r+',encoding='utf-8') as fd,open(des_file1,'w+',encoding='utf-8') as fd1:
for line in fd:
if old in line:
new_line = line.replace(old,new)
else:
new_line = line
print(new_line)
fd1.write(new_line) def main():
result = usage(parameter)
if result:
if check_string_exist(result[1],result[3]):
replace(result[1],result[2],result[3])
else:
print('the %s is not found in %s ' % (result[1],result[3]))
else:
exit('USAGE: %s old_str new_str des_file' % sys.argv[0]) if __name__ == '__main__':
main()

最新文章

  1. AX 2012 template table use in Query
  2. vim 查找时忽略大小写
  3. hdu 5444 Elven Postman
  4. 【转】rvm安装ruby,gem,rails,之后仍然无法找到rails命令
  5. ssh(Struts2+hibernate+spring)简单分页
  6. C# TypeConverter 数据转换
  7. 点击itemView选中checkbox
  8. jQuery 焦点图,图像文件js档
  9. unity 创建NGUI字体
  10. Unity3D NGUI事件监听的综合管理
  11. SQLALCHEMY_TRACK_MODIFICATIONS adds significant异常的解决方法
  12. c# 后台绑定treeview 多个tab
  13. CentOS7 YUM安装与配置 MySQL5.7
  14. linux中一些简便的命令之tr
  15. 修改Devexpress DateEdit控件默认的日期格式和日历风格
  16. 数据库sql优化总结之1-百万级数据库优化方案+案例分析
  17. 通过父类定位到子类 先将父类当作一个dom
  18. POJ3581:Sequence——题解
  19. 2014蓝桥杯B组初赛试题《李白打酒》
  20. C++11写算法之插入排序

热门文章

  1. hdu1506 Largest Rectangle in a Histogram
  2. 第七篇数字&字符串之练习题
  3. 剑指offer-跳台阶08
  4. 剑指offer-斐波那契数列07
  5. pandas DataFrame的查询方法(loc,iloc,at,iat,ix的用法和区别)
  6. for循环再探
  7. SpringBoot 中使用shiro注解使之生效
  8. C#中的Stack的Peek操作,曝出异常
  9. systemtap get var of the tracepoing
  10. java生成和解析二维码