python3 实现类似sed修改文件功能
#! /usr/bin/env python3 # -*- encoding:utf-8 -*- import sys,os find_str = sys.argv[1] replace = sys.argv[2] file_name = sys.argv[3] f1 = open(file=file_name,mode='r',encoding='utf-8') f2 = open(file=file_name+'_new',mode='w',encoding='utf-8') for item1 in f1: if 'first' in item1: item1 = item1.replace(find_str,replace) f2.write(item1) f1.close() f2.close() os.remove(file_name) os.rename(file_name+'_new',file_name) """ import sys,os src = sys.argv[1] des = sys.argv[2] file_name = sys.argv[3] with open(file=file_name,mode='r') as f, open(file=file_name+'_new',mode='w') as f_new: for i in f: if src in i: i = i.replace(src,des) f_new.write(i) os.remove(file_name) os.rename(file_name+'_new',file_name) """
相关推荐
chuckchen 2020-10-31
Will0 2020-10-12
Dreamhome 2020-10-09
xirongxudlut 2020-09-28
星辰大海的路上 2020-09-13
chaochao 2020-08-31
猪猪侠喜欢躲猫猫 2020-08-17
快递小可 2020-08-16
shengge0 2020-07-26
巩庆奎 2020-07-21
张文倩数据库学生 2020-07-19
xirongxudlut 2020-07-18
Ericbig 2020-07-18
kyelu 2020-07-09
liangzhouqu 2020-07-07
GuoSir 2020-06-28
chaigang 2020-06-27
pythonxuexi 2020-06-25