Reading input from file,editing file, and rewriting to file

I have a text file with just numbers, as such : 75, 7, 8, 115, 72, 102, 11, 25, 80 # dr.txt
I need to input these numbers (as one group) then subtract 1 from each number in the file, then
rewrite the file with the new numbers.
I have tried the following:

path = 'dr.txt'
with open(path, "r+") as f:
	for line in f.readline().splitlines():
		split_line = line.split(",")
		split_line = str(int(split_line) - 1)

with open(path, 'w') as file:
	for line in data:
		newnumbers = ','.join(line)

but I can't seem to get it to work.
I have seen something similar on this forum but could not make sense of it to adapt it to my
particular problem.
Any guidance is greatly appreciated.
Thank you.
Python 3.6 user


path = 'dr.txt'

will work only if the file is in the same folder as the script, which I’m assuming is the case.

open defaults to opening as text for reading, so you can say just:

with open(path) as f:

What you’re doing next is reading a line then splitting that line into lines(!) and iterating over them.

To iterate over the lines just iterate over the file:

for line in f:


split_line = line.split(",")

splits on commas, returning a list of strings, so this:

split_line = str(int(split_line) - 1)

is trying to convert a list into an int, which isn’t possible.

You need to convert each member of the list:

split_line = [str(int(number) - 1) for number in split_line]

Then you’re appending to data, which doesn’t exist. You need add:

data = []

And finally, when you’re writing the output to a file, you’re not adding the ‘\n’ at the end of each line.

try this code:

path = 'dr.txt'
data = []
with open(path, "r", encoding='utf-8') as f:
    for line in f.readlines():
        line = line.replace(' ', '')
        line = line.replace('\n', '')
        line = line.split(",")
        new_line = [str(int(value) - 1) for value in line]
with open(path, 'w') as file:
    result = ''
    for line in data:
        newnumbers = ','.join(line)
        result += newnumbers + '\n'

Is there one row of numbers or several rows?

There is only 1 row of numbers (9 elements).
When any of the numbers == 0, it gets reset to a preset starting number

Thank you very much for your input.
I understand better now why it wasn’t working and I will implement your
ideas as I progress further down this Python rabbit hole.


I tried your code and it worked perfectly. Thank you so much.
I understand better now why it wasn’t working.

Thanks again,

In this case I would just read, split on comma, convert to int and do math operation, go to start of file and print back into file with comma separator:

with open("dr.txt", "r+") as f:
    stream = (int(num) - 1 for num in', '))
    print(*stream, sep=", ", file=f)

EDIT: added f.truncate() as pointed out by Matthew Barnett: " the file might be shorter than what’s there already, e.g. writing 99 over the top of 100 will leave you with 990 , so you need to add f.truncate() just before closing the file"

@Alvar: There’s a small oversight there. What you write to the file might be shorter than what’s there already, e.g. writing 99 over the top of 100 will leave you with 990, so you need to add f.truncate() just before closing the file.

Thanks for your help.

I’m used to bash, but i like python more everyday, once I get the basics, I think I can make it work for all my “little projects”.


Yes, you are absolutely right. Mea culpa