Python Search text file & replace
Question: I want to take a list of words from textfile1.txt and replace the word
"example" on textfile2.txt to whatever the text is on line one, line two and
so on.. How would I do this?

Text file textfile1.txt

    
    
    user1
    user2
    user3
    user4
    user5
    

Text file textfile2.txt

    
    
    url/example
    url/example
    url/example
    url/example
    url/example
    

What I have so far

    
    
    #!/usr/bin/env python3
    import fileinput
    
    with fileinput.FileInput("textfile2.txt", inplace=True ) as file:
        for line in file:
            print(line.replace("example", "user1"), end='')
    

My goal:

    
    
    url/user1
    url/user2
    url/user3
    


Answer: This should do it. In general, when you want to traverse 2 or more iterables
(lists, files, etc) in parallel, odds are you can use `zip`.

    
    
    with open('textfile1.txt') as f1, open('textfile2.txt') as f2:
        for l, r in zip(f1, f2):
            print(r[:r.find('/')+1] + l)
    

