首页 技术 正文
技术 2022年11月13日
0 收藏 437 点赞 2,569 浏览 1293 个字
 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模块 替换输入内容脚本

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:8,991
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,505
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,349
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,134
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,766
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,844