0

I have not written any code or anything yet, because I have no idea where to even begin. But here is the basic idea. Suppose this is the content of the text file:

#hello
Hello there
#hi
Hiya

How should I write a Python code so that the program reads through the file and stops the cursor when it reaches the position immediately after #hello ? (P.S: I know how to open and close text files, and have written data into files and read files through Python as well. I'm new to this site, so do let me know if the question can be improved in any way.

martineau
  • 119,623
  • 25
  • 170
  • 301
  • The question really is what would you do after you've stopped the cursor (file position) there, since just having the file position there won't really do much. If you plan on adding new lines there, that's not going to work; you can only overwrite the following content, not insert content in-between (without manually reading the rest in, of course). – AKX Feb 11 '22 at 15:25
  • By cursor, do you mean the user input text cursor or the file position cursor? – Thomas Weller Feb 11 '22 at 15:26
  • @AKX my aim was to be able to add something like "Howdy" after '#hello' and before 'Hello there'. However, if that is impossible, then I will have to consider rewriting the file. Thanks for the help! (P.S: Is there any way to cut the total file in two, with everything before the required position being a string and everything after it being another string?) –  Feb 11 '22 at 15:28
  • @ThomasWeller well, my aim is to insert text right after that word. I have no idea what the semantics with this case are, sorry. –  Feb 11 '22 at 15:29
  • @ShadowReptile Yeah, unfortunately that's not going to work; you could overwrite the word `Hello` (5 characters) with `Howdy` (5 characters), or if you wrote `Hi`, you'd get `Hillo`, but you can't insert content in a file in a way that'd shift the rest of the file forward. – AKX Feb 11 '22 at 15:31
  • @flyingdutchman I hadn't seen it before, but I still can't understand anything from there. Is there a way to cut the text in the file into two strings: before the required position and after the required position? –  Feb 11 '22 at 15:32
  • You may have to scan the whole file into a string and use `.index()` or iterate through it word by word, modify it and overwrite the file. – Codeman Feb 11 '22 at 15:39
  • you could loop untill the line read contains the required text –  Feb 11 '22 at 16:18
  • Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. – Community Feb 22 '22 at 07:55

1 Answers1

1

Here's my approach (I'm converting the text file to a string so operating on it will be easier):

The text file:

#Hello!
----
This text will be replaced!
----
A line will be added above this!

The code:

with open('filepath/file.txt', 'r+') as f: 
    content = f.read()
    f.seek(0)  # You could also use two with statements instead of this
    content = content.replace('This text will be replaced!', 'Replaced!')
    content = content.replace('A line will be added above this!', 'Line added!\nA line will be added above this!')
    f.write(content)
    f.truncate()

Here is how to do it with two with open() as statements.

The text file after running code:

#Hello!
----
Replaced!
----
Line added!
A line will be added above this!

Does this answer your question? I know this doesn't exactly answer the question, but this answer is based off of the comment you gave about rewriting lines.

Codeman
  • 477
  • 3
  • 13