in this tutorial we'll learn how to write a script whitch replace text in file

in this example we'll read a file then replace "world" by "country" finaly we'll wirte the result in our file and save it

example


#openfile
with open('file.txt', 'r+') as f:
    file_source = f.read()
    
    #replace 'world' by 'country' in file
    replace_string = file_source.replace('world', 'country')
    
    #save change
    write_file = f.write(replace_string)


#read file after changing
with open('file.txt', 'r+') as f:
    #print
    print(f.read())

output

hello country

English today is not an art to be mastered it's just a tool to use to get a result