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

本博客只记录个人的学习过程,如对文中有不懂的地方,可以在评论区留言!
原文地址:https://www.cnblogs.com/iexperience/p/9022306.html