练习题1 —— 全局替换程序

# 练习题1 —— 全局替换程序:
# 写一个脚本,允许用户按以下方式执行时,即可以对指定文件内容进行全局替换
# `python your_script.py old_str new_str filename`
# 替换完毕后打印替换了多少处内容

import sys
# print('参数个数:', len(sys.argv))
# print('他们是:', str(sys.argv))

par = sys.argv #从命令行 获取 用户输入的参数的列表
path_list = sys.path #获取路径列表
# 使用占 内存方式修改文件
def replace_file(old_str,new_str,path_filename):
with open(path_filename,'r+',encoding='utf_8') as read_f:
info = ''
count = 0
for line in read_f:
# print(line,end='')
if old_str in line:
new_info = line.replace(old_str, new_str)
info += new_info
count += 1
else:
info += line
print('替换次数为%d' % count)
read_f.seek(0) #使光标移动到文件开头
read_f.write(info) #写入内容
read_f.truncate(read_f.tell()) # 删除掉光标当前位置 之后的内容! 防止解码错误
old_str = par[1]
new_str = par[2]
filename = '%s\%s' % (path_list[0],par[3])
replace_file(old_str, new_str, filename)

猜你喜欢

转载自www.cnblogs.com/chengege/p/10211862.html