0

Im pretty new to Python, but I've been trying to get into some programming in my free time. Currently, im dealing with the following problem:

I have 2 documents, 1 and 2. Both have text in them.

I want to search document 1 for a specific string. When I locate that string, I want to insert all the content of document 2 in a line after the specific string.

Before insertion: Document 1 content: text... SpecificString text...

After insertion: Document 1 content: text... SpecificString Document 2 content text...

I've been trying different methods, but none are working, and keep deleting all content from document 1 and replacing it. Youtube & Google haven't yielded any desireble results, maybe im just looking in the wrong places.

I tried differnet things, this is 1 example:

f1 = '/Users/Win10/Desktop/Pythonprojects/oldfile.txt'
f2 = '/Users/Win10/Desktop/Pythonprojects/newfile.txt'
searchString=str("<\module>")

with open(f1, "r") as moduleinfo, open(f2, "w") as newproject:
    new_contents = newproject.readlines()
    #Now prev_contents is a list of strings and you may add the new line to this list at any position
if searchString in f1:
new_contents.insert(0,"\n")
new_contents.insert(0,moduleinfo)
    #new_file.write("\n".join(new_contents))

The code simply deleted the content of document 1.

3 Answers3

1

You can find interesting answers (How do I write to the middle of a text file while reading its contents?, Can you write to the middle of a file in python?, Adding lines after specific line)

By the way, an interesting way is to iterate the file in a read mode to find the index where the insert must be. Afterwards, overwrite the file with new indexing:

a) File2 = File2[:key_index] + File1 + File 2[key_index:]

Another option explained by Adding lines after specific line:

with open(file, "r") as in_file:
    buf = in_file.readlines()

with open(file, "w") as out_file:
    for line in buf:
        if line == "YOUR SEARCH\n":
            line = line + "Include below\n"
        out_file.write(line)

Please tell us your final approach.

KR,

0

You have to import the second file in append mode instead of writing mode. Write mode override the document. Append mode add text to the end of the file, but you can move the pointer to the wanted location for writing and append the text there.

You can enter append mode by replacing the 'w' with 'a'.

nokla
  • 451
  • 1
  • 8
0

Thanks for your input, it put me on the right track. I ended up going with the following:

f2 = '/Users/Win10/Desktop/Pythonprojects/newfile.txt'
f1 = '/Users/Win10/Desktop/Pythonprojects/oldfile.txt'

with open(f2) as file: 
    original = file.read()
with open(f1) as input:
    myinsert = input.read()

newfile = original.replace("</Module>", "</Module>\n"+myinsert)

with open(f2, "w") as replaced:
    replaced.write(newfile)

text from oldfile is inserted into newfile in a new line, under the "/Module" string. I'll be following up, if I find better solutions. Again, thank you for your answers.