马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
"""replace strings in text"""
import os
def Replace(file_name, rep_word, new_word):
with open(file_name) as f:
content = []
count = 0
for eachline in f:
if rep_word in eachline:
count += eachline.count(rep_word)
eachline = eachline.replace(rep_word, new_word)
content.append(eachline)
decide = input('文件 {0} 中共有{1}个【{2}】\n您确定要把所有的【{3}】替换为【{4}】吗?\n【YES/NO】:'.format\
(file_name, count, rep_word, rep_word, new_word))
if decide in ['YES', 'Yes', 'yes']:
with open(file_name, 'w') as f:
f.writelines(content)
print('Succeed!')
else:
print('Exit!')
if __name__ == '__main__':
while True:
file_name = input('请输入文件名:')
if file_name in os.listdir():
rep_word = input('请输入需要替换的单词或字符:')
new_word = input('请输入新的单词或字符:')
Replace(file_name, rep_word, new_word)
break
else:
print('Do not find such a file {}'.format(file_name))
|