python file模块 替换输入内容脚本
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()

python file模块 替换输入内容脚本的更多相关文章
- 批量替换存储过程内容脚本sp_SqlReplace
开始 在数据库开发过程中,如果某一个表字段名被重命名.那么在使用到该字段的存储过程,对应的表字段名也要修改. 当存在多个存储都有使用该表字段,需要逐个去修改替换,是一件比较繁琐的事情,我们需要一个能实 ...
- python全局替换文件内容脚本第1版
#!/usr/bin/python #coding=utf8 """ # Author: xiaoyafei # Created Time : 2018-05-08 09 ...
- python中从键盘输入内容的方法raw_input()和input()的区别
raw_input()输出结果都是字符串 Input()输入什么内容,输出就是什么内容
- Python sh模块--------替换subprocess的利器
官方文档有句话"allows you to call any program",并且: helps you write shell scripts in Python by giv ...
- 孤荷凌寒自学python第三十五天python的文件操作之针对文件操作的os模块的相关内容
孤荷凌寒自学python第三十五天python的文件操作之针对文件操作的os模块的相关内容 (完整学习过程屏幕记录视频地址在文末,手写笔记在文末) 一.打开文件后,要务必记得关闭,所以一般的写法应当 ...
- python 操作excel实现替换特定内容
本文介绍使用python语言,借助openyxl库来实现操作excel(xlsx)文件,实现替换特定内容的需求. 目前实现了3个小功能: 1. 全字匹配替换(mode1):(如:全字匹配 yocich ...
- Python基于正则表达式实现文件内容替换的方法
Python基于正则表达式实现文件内容替换的方法 本文实例讲述了Python基于正则表达式实现文件内容替换的方法.分享给大家供大家参考,具体如下: 最近因为有一个项目需要从普通的服务器移植到SAE,而 ...
- python file对象测试数据的读写操作及OS模块介绍(四)
import from....import 引入模块 引入类 ①import 如果文件在lib下而且是python模块 :import 模块名. ②from....import from 包名.包 ...
- Python+Selenium自动化-清空输入框、输入内容、点击按钮
Python+Selenium自动化-清空输入框.输入内容.点击按钮 1.输入内容 send_keys('valve'):输入内容valve #定位输入框 input_box = browser. ...
随机推荐
- select 支持宽高(高度有兼容问题);
<select size=1(默认) size=2 没有下拉效果> <option selected>12</option> <option selected ...
- android 第三方 图表
1.XCL-Charts 直接利用Canvas api画出图形,各有好处. XCL-Chart尽量把图的绘制逻辑封装在类中,而把绘制相关的各 个元素开放出来,目的是在保证开发效率的同时,给程序员足够多 ...
- 【BZOJ】2924: [Poi1998]Flat broken lines
题意 平面上有\(n\)个点,如果两个点的线段与\(x\)轴的角在\([-45^{\circ}, 45^{\circ}]\),则两个点可以连线.求最少的折线(折线由线段首尾相连)使得覆盖所有点. 分析 ...
- fuser 命令的介绍
fuser 可以显示出当前哪个程序在使用磁盘上的某个文件.挂载点.甚至网络端口,并给出程序进程的详细信息. fuser显示使用指定文件或者文件系统的进程ID.默认情况下每个文件名后面跟一个字母表示访 ...
- ffmpeg入门
总入口 http://blog.csdn.net/leixiaohua1020/article/details/15811977 各结构体介绍 http://blog.csdn.net/leixiao ...
- mysql相似于oracle的to_char() to_date()方法
mysql日期和字符相互转换方法, date_format(date,'%Y-%m-%d') -------------->oracle中的to_char(); str_to_date(dat ...
- 32位的Win7系统下安装64位的Sql Sever?
来自:http://zhidao.baidu.com/link?url=nQBoaLgoOyYCUdI7V4WZCMlTW3tKscdkOnLTIvlYtPpwoVhQkSahq44HeofBfzFT ...
- H5学习小结——div+css创建电子商务静态网页
使用Sublime Text软件编写电子商务类网站静态形式首页 经过差不多一星期的学习,基本掌握了div+css的用法之后,开始了实战练习.首先要做的就是要练习一下一般电子商务网页的编写,我做的是下图 ...
- webapp 公共样式
html{ font-size: 62.5%;}@media only screen and (min-width: 481px) { html { font-size:94%!important } ...
- HTML5初学篇章_4
HTML5的表单所有type类型(补第一章) 类型 说明 button 定义可点击的按钮(大多与 JavaScript 使用来启动脚本) checkbox 定义复选框. color 定义拾色器. da ...