|
发表于 2017-4-4 16:13:11
|
显示全部楼层
def find_eachline(str,character):
lis = str.split(character)
num = len(lis) - 1
return num
def find_all(file,character):
f = open(file)
flag = 0
for eachline in f:
n = find_eachline(eachline,character)
flag += n
f.close()
return flag
def all_replace(file,old,new):
n = find_all(file,old)
f = open(file)
print("There are %d %s"%(n,old))
choice = input("Are you sure to replace %s with %s 【Y/N】?"%(old,new))
temp = []
if choice == 'Y':
for eachline in f:
temp.append(eachline.replace(old,new))
f.close()
f = open(file,'w')
f.writelines(temp)
f.close()
file = input("Please enter filename: ")
old = input("Please enter the characters will be replaced: ")
new = input("Please enter the new character: ")
all_replace(file,old,new) |
|