Day13 【小程序】文件修改

文件修改方法:1.加载到内存中修改;2.打开一个文件,修改后写到新文件中

f=open('yesterday','r',encoding='utf-8')

f_new=open('yesterday.bak','w',encoding='utf-8')
find_str=input('Find words:')
replace_str=input('Replace words:')
for line in f: if find_str in line: line=line.replace(find_str,replace_str) f_new.write(line) f.close() f_new.close()

 通过脚本传参数的方法

import sys

f=open('yesterday','r',encoding='utf-8')
f_new=open('yesterday.bak','w',encoding='utf-8')

find_str=sys.argv[1]
replace_str=sys.argv[2]
for line in f:
    if find_str in line:
        line=line.replace(find_str,replace_str)
    f_new.write(line)

 

posted @ 2018-04-20 23:29  q1ang  阅读(223)  评论(0编辑  收藏  举报