Copy one file content (data) to other file file in the specific area using python Logic -


i have experience in c programming languange , started work on python c backround.

we have file names example 1.txt , 2.txt. taking content of 1.txt , writing in 2.txt given line number , lines in 2.txt moved down automtically - using below logic:

f = open("1.txt", "r") contents = f.readlines() f.close()  open("2.txt") f:     data = f.read().rstrip("\n")     line =19 contents.insert(line, data)  f = open("1.txt", "w") contents = "".join(contents) f.write(contents) f.close() 

this logic working problem facing last line written immideatly after 1.txt content. ( per requiremnt has start form next line) tried "\n" not able achive.

for more clarity below.

1.txt :

   hello     hello     hello     hello 

2.txt:

123 456 789 451 234 

suppose if execute code above 1.txt , 2.txt files ( , want write data between 789 , 451).

after executing script 2.txt looks below:

123 456 789 hello hello hello hello 451 234 

here "hello 451" making logic not work next time.

so want achive below:

123 456 789 hello hello hello hello  451 

please kinldy share solution or guide me achive logic. 234

i made 1 change in code , worked me

replace line : contents.insert(line, data) : contents.insert(line, data+"\n")


Comments

Popular posts from this blog

networking - Vagrant-provisioned VirtualBox VM is not reachable from Ubuntu host -

c# - ASP.NET Core - There is already an object named 'AspNetRoles' in the database -

ruby on rails - ArgumentError: Missing host to link to! Please provide the :host parameter, set default_url_options[:host], or set :only_path to true -