标签:ace 重命名 content 内容 txt open lin NPU style
1 f1 = open("hello.txt","w") 2 f1.write("hello,my name is bobo.") 3 f1.close() 4 def func(): 5 name = input("输入名字:") 6 content = input("输入内容:") 7 new_content = input("输入文件新内容:") 8 new_name = name + ".new" 9 import os 10 f = open(name,"r") 11 f_new = open(new_name,"w") 12 for line in f: 13 if content in line: 14 new_line = line.replace(content,new_content) 15 else: 16 new_line = line 17 f_new.write(new_line) 18 f.close() 19 f_new.close() 20 os.replace(new_name,name) # 替换replace不会报错 21 func() #rename 重命名name是目录里已存在的文件, 则会报错
标签:ace 重命名 content 内容 txt open lin NPU style
原文地址:https://www.cnblogs.com/joneylulu/p/10294448.html