Python Forum
Replace lines from one .txt file to another
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Replace lines from one .txt file to another
#1
I am a beginner to do any coding, but still i'll try to do something..  Smile

My problem:

i need copy some lines Start.txt to end.txt, only what i know is line numbers, marks/numbers inside the line doest not same all the time, line numbers are. So i cant use .replace method?

Example

Start.txt
       1111111111
       2222222222
       3333333333
       4444444444  <------ This line
       5555555555  <------ And this line
End.txt
       888888888
       999999999
       AAAAAAAAA
       BBBBBBBBB
                 <----- to here
                 <----- to here
i was find some enumerate example before, and try to adjust it, but too hard from beginner.
Something like this what i find:

       with open('start.txt') as fin, open('end.txt', 'w') as fout:
           for i, item in enumerate(fin, 1):
               if i == 7: 
                   item = "string\n" 
               fout.write(item)
This code add only "string" word to line which number i gave...


Thanks!
Reply


Messages In This Thread
Replace lines from one .txt file to another - by Tumppi - Jun-21-2017, 07:34 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Replace a text/word in docx file using Python Devan 4 4,115 Oct-17-2023, 06:03 PM
Last Post: Devan
  Need to replace a string with a file (HTML file) tester_V 1 854 Aug-30-2023, 03:42 AM
Last Post: Larz60+
  Replace columns indexes reading a XSLX file Larry1888 2 1,057 Nov-18-2022, 10:16 PM
Last Post: Pedroski55
  Delete multiple lines from txt file Lky 6 2,440 Jul-10-2022, 12:09 PM
Last Post: jefsummers
  How to do a mass replace within a CSV file? cubangt 9 5,651 May-09-2022, 06:52 PM
Last Post: snippsat
  I get an FileNotFouerror while try to open(file,"rt"). My goal is to replace str decoded 1 1,470 May-06-2022, 01:44 PM
Last Post: Larz60+
  failing to print not matched lines from second file tester_V 14 6,355 Apr-05-2022, 11:56 AM
Last Post: codinglearner
  Extracting Specific Lines from text file based on content. jokerfmj 8 3,219 Mar-28-2022, 03:38 PM
Last Post: snippsat
  Importing a function from another file runs the old lines also dedesssse 6 2,696 Jul-06-2021, 07:04 PM
Last Post: deanhystad
  [Solved] Trying to read specific lines from a file Laplace12 7 3,676 Jun-21-2021, 11:15 AM
Last Post: Laplace12

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020