零基础学Python课后题:文件中“全部替换”的题
def file_replace(file_name, rep_word, nwe_word):f_read = open(file_name, encoding = 'utf-8')
content = []
count = 0
for eachline in f_read:
if rep_word in eachline:
count += eachline.count(rep_word)
eachline = eachline.replace(rep_word, new_word)
content.append(eachline)
decide = input('\n文件%s中共有%s个【%s】\n您确定要把所有的【%s】替换为【%s】吗?\n【YES/NO】:'%(file_name, count, rep_word, rep_word, new_word))
#print(content)
if decide in ['YES', 'Yes', 'yes']:
f_write = open(file_name, 'w', encoding = 'utf-8')
f_write.writelines(content)
f_write.closs()
f_read.close()
file_name = input('请输入文件名:')
rep_word = input('请输入需要替换的单词或字符:')
new_word = input('请输入新的单词或字符:')
file_replace(file_name, rep_word, new_word)
按照答案敲的代码,但是运行的时候出现错误:Traceback (most recent call last):
File "C:/Users/1/Desktop/Python/45/29/replace.py", line 40, in <module>
file_replace(file_name, rep_word, new_word)
File "C:/Users/1/Desktop/Python/45/29/replace.py", line 33, in file_replace
f_write.closs()
AttributeError: '_io.TextIOWrapper' object has no attribute 'closs'
萌新求教,麻烦各位大佬解答 拼写错误,close打成了closs 看最后一行报错,代码倒数第二行close()错写成closs()了。 昨非 发表于 2020-10-13 19:59
拼写错误,close打成了closs
好的,看到了,谢谢大佬 hrp 发表于 2020-10-13 19:59
看最后一行报错,代码倒数第二行close()错写成closs()了。
好的,谢谢大佬 1519942484 发表于 2020-10-13 20:20
好的,看到了,谢谢大佬
好吧
页:
[1]