How do I write to the middle of a text file while reading its contents?

前端 未结 3 1449
时光取名叫无心
时光取名叫无心 2020-12-06 22:03

First of all thanks for helping me out with moving files and for helping me in with tcl script.

Small doubt i had with python code.. as below..

impor         


        
3条回答
  •  春和景丽
    2020-12-06 22:43

    you dont all you can do is read in the file, insert the text you want, and write it back out

    with open("some_file","r") as f:
         data = f.read()
    some_index_you_want_to_insert_at = 122
    some_text_to_insert = "anything goes here"
    
    new_data = data[:some_index_you_want_to_insert_at] + some_text_to_insert + data[some_index_you_want_to_insert_at:]
    
    with open("some_file","w") as f:
         f.write(new_data)
    
    print "all done!"
    

提交回复
热议问题