-3

I would like to read one large file containing lines of switch commands and then start a new text file containing the same list of text commands, but only replacing a few characters or lines of text with the desired new text.

I would also like to have conditionals, meaning if one specified line of text is present then I would like a desired text to be replaced in it's place in the new file.

I am using Visual Studio Code

rioV8
  • 24,506
  • 3
  • 32
  • 49
sammysosa
  • 1
  • 4
  • 1
    What exactly have you tried? To read files use `with open("file.txt", "r") as file:` and to write something use `with open("file.txt", "w") as file:`. – CozyCode Dec 12 '21 at 21:40
  • @CozyCode I have tried this original_file = open('SanitizedFinal_E4300.txt','r') revised_file = open('test1.txt','w') for aline in original_file: revised_file.write('this is a new line') original_file.close() revised_file.close() However it replaced all the lines with "this is a new line" – sammysosa Dec 12 '21 at 21:42
  • 1
    What exactly do you want to do? If you want to change only a few lines then simply read that file change the string then write everything to it. – CozyCode Dec 12 '21 at 21:53
  • well you are writing "this is a new line" to the new file for every line in the original; try `revised_file.write(aline)` instead, and a few conditions perhaps if you want to change it – seldomspeechless Dec 12 '21 at 22:00
  • @CozyCode Essentially what I want to do is: If a specific text is present I would like to add another line od text into a new file containing all the original text. locate then replace/add. – sammysosa Dec 12 '21 at 22:00
  • See why your code isn't doing what you expected [here](https://stackoverflow.com/a/1466036/11177720). – 12944qwerty Dec 12 '21 at 22:04
  • @CozyCode think using `with open() as file:` is good practice, but if you use both at the same time you obviously can't both have them called file, preferably something selfexplainatory like fin & fout – seldomspeechless Dec 12 '21 at 22:08
  • @seldomspeechless can you show me an example with conditions? – sammysosa Dec 13 '21 at 03:09
  • Thank you, it worked for me partially. How would I approach it if all the lines started with "some data", and if I wanted to replace just a few lines? elif statement doesn't seem to work for me in this situation. @seldomspeechless – sammysosa Dec 20 '21 at 20:10
  • Yes, I wrote the correct file names. Could you test the code and let me know what to change? @CozyCode – sammysosa Dec 20 '21 at 23:41

2 Answers2

0

So do you want something like this:

output = ""

with open("1.txt", "r") as file:
    for word in file.read().split():
        if word == "Test":
            output += "test3"
        elif word == "321":
            output += "#new value2"
        else:
            output += word

with open("1.txt", "w") as file:
    file.write(output)
CozyCode
  • 484
  • 4
  • 13
  • output = "" original_file = () with open("SanitizedFinal_E4300.txt", "r") as file: original_file = file.readlines() for line in original_file: for word in line: if word == "set system host-name": output += "new value" elif word == "set system auto-snapshot": output += "new value2" else: output += word with open("file1.txt", "w") as file: file.write(output) @CozyCode It created a new file but the text didn't update. – sammysosa Dec 13 '21 at 02:50
  • Are you sure you wrote the right file names? The first one is the file you want to get the original text from and the second one is the file you want to write to. If you want to update the same file then right the file name to both the reading and writing. – CozyCode Dec 13 '21 at 10:15
  • Yes, I wrote the correct file names. Could you test the code and let me know what to change? @Cozy Code – sammysosa Dec 20 '21 at 23:40
  • Ok sorry I ran the code and made some modifications. It should work now :) – CozyCode Dec 21 '21 at 07:32
0

So here is a short example:

with open("data.txt","r") as fin:
    with open("output2.txt","w") as fout:
        for aline in fin:
            new_data = aline
            if new_data.startswith("some data"):
                new_data = "replacement=string"
            elif "coffeebreak" in new_data:
                new_data = "coffee is schedualed at 10 on the 15th"
            fout.write(new_data+"\n") # You would want to end the line with a linebreak.

Contents of "data.txt":

some data we are logging
some data we are logging
we are having coffeebreak sometime soon
some data we are logging
something about the brown fox

Output in our second file "output2.txt":

replacement=string
replacement=string
coffee is schedualed at 10 on the 15th
replacement=string
something about the brown fox

There are many benefits of using with such as no need to close files afterwards.

Hope this works out fine for you!

  • Thank you, it worked for me partially. How would I approach it if all the lines started with "some data", and if I wanted to replace just a few lines? elif statement doesn't seem to work for me in this situation. – sammysosa Dec 19 '21 at 23:56
  • Thank you, it worked for me partially. How would I approach it if all the lines started with "some data", and if I wanted to replace just a few lines? elif statement doesn't seem to work for me in this situation. @seldomspeechless – sammysosa Dec 20 '21 at 20:10