Replacing a part of certain line # with input from another file

I need to replace the numbers(only) from line # 21 to the new numbers from another file
example 74,9,7,114,71,101,10,24,79

I have tried:

with open('rxtraxconfig.py', 'a') as f1:      # screenshot file

    for line in open('daysremaining.txt'):   # file containing new numbers

        if 'days_remaining = ' in line:

            f1.write(line)

to no vail.
suggestions are greatly appreciated
thank you,
cogiz
python 3.6 user

Opening for appending will do just that, let you append, i.e. add new stuff to the end of a file.

What you are actually doing is iterating over the lines in ‘daysremaining.txt’ and then appending to ‘rxtraxconfig.py’ if any line that’s in ‘daysremaining.txt’ contains 'days_remaining = '.

What you need to do is copy lines from the original file to a new file, making any changes in the process:

with open('rxtraxconfig.py') as input_file:
    with open('rxtraxconfig.py.new', 'w') as output_file:
        for line in input_file:
            if 'days_remaining = ' in line:
                # Make the changes to the line.
                ...

            output_file.write(line)

# Now you can replace the old file with the new file.
...

thank you very much for your suggestion.

I was able to do what I wanted with the following:


	with contextlib.redirect_stdout(None):
		with open('daysremaining.txt') as f:
			line = f.readline()
		print (line)
		print (line[0])


	with contextlib.redirect_stdout(None):
		with open('rxtraxconfig.py') as f:
			data = f.readlines()
		print (data)
		print (data[0]) 
		newdata = "	days_remaining = "
		newdata2 = (line)
		data[28] = str(newdata) + str(newdata2) 


	with open("rxtraxconfig.py", 'w') as f:
	
		f.writelines( data ) 
		f.close()

thanks again,
regards,
cogiz
python 3.6 user