0
import os

word_to_replace, replacement_word = "", ""                                                     

if (os.path.isfile('data_association/names.txt')):
    word_file_path = 'data_association/names.txt'
else:
    open('data_association/names.txt', "w")
    word_file_path = 'data_association/names.txt'

word = "Claudio"

with open(word_file_path) as f:
    lineas = [linea.strip() for linea in f.readlines()]
    numero = None
    if word in lineas: numero = lineas.index(word)+1

    if numero != None:
        #Here you must remove the line with the name of that file
    else: print(That person's name could not be found within the file, so no line has been removed)

I need to try to remove that name from the following .txt file (assuming that there is a line with that name in that file, if it is a file without that line it should print that that name has not been found and that it cannot be deleted)

Lucy
Samuel
María del Pilar
Claudia
Claudio
Katherine
Maríne

After removing the line with the example name "Claudio", the file would look like this:

Lucy
Samuel
María del Pilar
Claudia
Katherine
Maríne

PS: this list of names is a reduced fragment of the actual list, so it's better if instead of rewriting the whole file, you just edit the specific line where that name was found

Matt095
  • 857
  • 3
  • 9
  • 1
    You just have to delete the item from "lineas" and then write "lineas" to the .txt file. What is the specific problem? – Michael Butscher Jan 14 '23 at 05:48
  • @MichaelButscher I was asking for a way where you are not forced to rewrite the whole file, because in long files that would take too much time. I would like to know if I can remove the specific line (in case it is found), without having to remove all the lines to rewrite all the lines again. I did not see the need to clarify it within the question, but the list of names that is placed there is much smaller than the real list. – Matt095 Jan 14 '23 at 05:51
  • 1
    I think it would be a good idea to include this requirement in your post – Aemyl Jan 14 '23 at 05:53
  • 1
    You have to newly write the file at least from the point where the name is up to the end of the file. It is not possible to remove or insert bytes in a file without doing that. – Michael Butscher Jan 14 '23 at 05:55
  • I have added with a P.S. that data to the question. I don't know if it is possible to simply edit the line, since I have managed to identify with my code the line where the word (the name) is. But now with that line number, I'm not sure if I can eliminate him directly with any method. The goal is to optimize the algorithm as much as possible, so that it has to rewrite as few lines as possible. – Matt095 Jan 14 '23 at 05:57
  • 1
    also, it looks like you try to create the file if it doesn't exist (correct me if I'm wrong about that). It would be better to wrap the code inside a function and do an early return / raise an Exception if `os.path.exists(file_path)` returns `False` (guard clause). With your current code you have the problem that you never close the file you open in writing mode – Aemyl Jan 14 '23 at 05:59
  • 1
    Alternatively to @Aemyl 's suggestion you can also just write `open(..., "w").close()` to reliably close the file (if no error occurs). – Michael Butscher Jan 14 '23 at 06:03
  • add this to avoid an error when no file is created, maybe if you need to change something on that line as well., with `open('data_association/names.txt', "w").close()` – Matt095 Jan 14 '23 at 06:05
  • @MichaelButscher that would definitely solve the problem of the file being left open, but if the purpose of this code line is what I assume, there are better ways to prevent the code from crashing because it attempts to open a file in reading mode that doesn't exist – Aemyl Jan 14 '23 at 06:06
  • @Aemyl It depends on the specific use case if a missing file should be created empty or no changes should happen. – Michael Butscher Jan 14 '23 at 06:11
  • @MichaelButscher that is true, if OP wants that empty file your suggestion is probably the cleanest solution – Aemyl Jan 14 '23 at 06:13
  • Maybe you could overwrite the name with something like e. g. "-------". This needs to be done carefully with respect to the character encoding of the file (e. g. the UTF encodings have a varying number of bytes per character). You could then provide a final cleanup function which rewrites the whole file to remove the overwritten names. – Michael Butscher Jan 14 '23 at 06:20
  • Mmmm with what they put I was able to solve the problem of closing the file with `open('data_association/names.txt', "w").close()`, although I can't get how to delete that line with the name :( – Matt095 Jan 14 '23 at 06:21
  • Similar SO post: https://stackoverflow.com/questions/4710067/how-to-delete-a-specific-line-in-a-file – frederick-douglas-pearce Jan 14 '23 at 07:21

0 Answers0