|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
- def file_replace(file_name,oldword,newword):
- f = open(file_name)
- count = 0
- string = f.read() #输出为字符串
- string.replace('\n','')
- f.seek(0,0)
- for each in string: #统计出现的次数
- if oldword == each:
- count += 1
- print('文件%s中共有%s个%s' %(file_name,count,oldword))
- temp = input('您确定要将所有的%s替换为%s吗? \n 【Yes\\No】' %(oldword,newword))
- if temp in ['yes','YES','Yes']: #确认是否更改
- string.replace(oldword,newword) #更改字符串
- f_new = open(file_name,'w') #重新覆盖写入
- f_new.write(string)
- f_new.close()
-
- f.close()
- file_name = input(r'请输入要打开的文件(C:\\test.txt):')
- oldword = input('请输入要替换的字符:')
- newword = input('请输入新的单词:')
- file_replace(file_name,oldword,newword)
复制代码
题目大意是将文件的某个字符更改为别的字符
改了很久,程序终于不报错了,但是文件的内容并没有更改 求解答 |
|