|
发表于 2018-12-30 22:43:48
|
显示全部楼层
我的版本
- file_name = input('请输入文件名:')
- old = input('请输入需要替换的单词或字符:')
- new = input('请输入新的单词或字符:')
- f = open(file_name)
- text_old = f.read()
- f.close()
- count = text_old.count(old)
- def ensure(file_name,old,new,count):
- print('\n')
- if count:
- print('文件{0}中共有{1}个【{2}】\n\
- 你确定要把所有的【{3}】替换为【{4}】吗?'.format(file_name,count,old,old,new))
- order = input('【YES/NO】:')
- if order in ('YES','yes'):
- return 1
- else:
- return 0
- else:
- print('文中没有【{0}】'.format(old))
- return 0
- def change(file_name,text_old,old,new):
- text_new = text_old.replace(old,new)
- file = open(file_name,'w')
- file.write(text_new)
- file.close()
- if ensure(file_name,old,new,count):
- change(file_name,text_old,old,new)
-
-
-
复制代码 |
|