Python Forum
update txt file but keep a specific line
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
update txt file but keep a specific line
#1
Hi,

I'd like run "an update a file from a template file" routine.

Setup:
target file (target.txt)
template file (template.txt)

Info:
target file has no value but for one important line ("InstallGUID").
target file can't be deleted, so must be altered.
num of lines in target file can vary from the template file, so longer is very much possible (and NOT what should happen, all info must be deleted BUT the InstallGUID line).

In easy language (how I do things right now using plain windows 10, 2x notepad):
Open target.txt search for line starts with 'keeper', select that line, ctrl+c
Open template.txt search for line starts with 'keeper', select that line, ctrl+v
Ctrl+a and ctrl+c in template.txt
Ctrl+a in ctrl+v in target.txt
close

My attempt & question:
Works but it doesn't rule out any possible cases where target.lines > template.lines. How to prevent that?

template = "template.txt"
target = "target.txt"
with open(target, 'r') as test:
	for t in test:
		if t.startswith("InstallGUID"):
			installguid = t
		
with open(template, 'r') as rf:
    with open(target, 'w+') as wf:        
		for line in rf:
			if line.startswith("InstallGUID"):
				wf.write(installguid)
			else:
				wf.write(line)
Reply
#2
I don't understand what can go wrong with your attempt. Can you give a concrete short example?
Reply
#3
Hi Gribouillis,

You are probably not wrong, I think I don't fully understand the write command.

I thought that:
if source file has 20 lines
and
target file has 30 lines
only 20 lines were overwritten by the source file.

But I ran a test and that didn't happen. The target file ended with 20 lines.

So I guess I have some catching up to do on that topic.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Extracting specific file from an archive tester_V 4 428 Jan-29-2024, 06:41 PM
Last Post: tester_V
  File "<string>", line 19, in <module> error is related to what? Frankduc 9 12,392 Mar-09-2023, 07:22 AM
Last Post: LocklearSusan
  Getting last line of each line occurrence in a file tester_V 1 812 Jan-31-2023, 09:29 PM
Last Post: deanhystad
  How to read csv file update matplotlib column chart regularly SamLiu 2 1,016 Jan-21-2023, 11:33 PM
Last Post: SamLiu
  Reading Specific Rows In a CSV File finndude 3 940 Dec-13-2022, 03:19 PM
Last Post: finndude
  Writing string to file results in one character per line RB76SFJPsJJDu3bMnwYM 4 1,306 Sep-27-2022, 01:38 PM
Last Post: buran
  How to extract specific data from .SRC (note pad file) Shinny_Shin 2 1,225 Jul-27-2022, 12:31 PM
Last Post: Larz60+
  Is it possible to update a CSS file from Python bigAL_python 4 2,203 Apr-20-2022, 01:38 PM
Last Post: bigAL_python
  Print to a New Line when Appending File DaveG 0 1,190 Mar-30-2022, 04:14 AM
Last Post: DaveG
  Extracting Specific Lines from text file based on content. jokerfmj 8 2,862 Mar-28-2022, 03:38 PM
Last Post: snippsat

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020