python基础实现简单的shell sed 替换功能

#coding:utf-8
from pygame.draw import lines
import sys,os
old_file = sys.argv[1]  #接受外部设备上的参数
new_file = sys.argv[2]
old_yes=open('yes.txt','r')
replace_yes=open('yes1.txt','a+')
lines=old_yes.readlines()   #将硬盘中的文件内容赋值给内存变量
# print lines
for line in lines:
#     print '1111111'
    if old_file in line:
         line=line.replace(old_file,new_file)  #替换字符
    replace_yes.write(line)
old_yes.close()
replace_yes.close()

执行后程序报:IndexError: list index out of range

这需要在命令行执行,需要带参数。python *.py 参数1

posted @ 2018-05-10 23:13  python|一路向前  阅读(415)  评论(0编辑  收藏  举报