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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值