1

I dont know why but with the code below everything works fine and all the text is taken out and then put back into the text file

def upgradecap():
yc = open("ycfile", 'r')
a = yc.readline()
b = yc.readline()
c = yc.readline()
d = yc.readline()
e = yc.readline()
f = yc.readline()
g = yc.readline()
h = yc.readline()
i = yc.readline()
j = yc.readline()
k = yc.readline()
cap = yc.readline()
cap = int(cap)
cap = cap + 2500
cap = str(cap)
l = yc.readline()
yc = open("ycfile", "w+")
yc.write(a)
yc.write(b)
yc.write(c)
yc.write(d)
yc.write(e)
yc.write(f)
yc.write(g)
yc.write(h)
yc.write(i)
yc.write(j)
yc.write(k)
yc.write(cap + '\n')
yc.write(l)
yc.close()
L62.configure(text=cap)

But the next line of code writes everything back to the file except from the last line of writing to the file in the second function

def upgradetrn():
yc = open("ycfile", 'r')
a = yc.readline()
b = yc.readline()
c = yc.readline()
d = yc.readline()
e = yc.readline()
f = yc.readline()
g = yc.readline()
h = yc.readline()
i = yc.readline()
j = yc.readline()
trn = yc.readline()
trn = int(trn)
trn = trn + 1
trn = str(trn)
k = yc.readline()
x = yc.readline()
yc = open("ycfile", "w+")
yc.write(a)
yc.write(b)
yc.write(c)
yc.write(d)
yc.write(e)
yc.write(f)
yc.write(g)
yc.write(h)
yc.write(i)
yc.write(j)
yc.write(trn + '\n')
yc.write(k)
yc.write(x)
yc.close()
L61.configure(text=trn)

All i am trying to do is take each line out of the text file and edit one line and then put it all back in. Does anyone know why this is happening? Thanks for any answers

Paul Rooney
  • 20,879
  • 9
  • 40
  • 61
maxpepsi
  • 33
  • 4

1 Answers1

2

Two things. Number 1, the problem.
As far as I understand, everything except the last write() call is not being written to the file?
This is because when you write to a file in 'w' or 'w+' mode, everything in that file is replaced by what you're writing to it.
So if I have a file with the word 'dog' in it, and then do this:

file.write('cat')
file.write('goldfish')

'dog' will be replaced by 'cat', then 'cat' by 'goldfish'. So all you have left is 'goldfish'.
To solve this, use the 'a' (append) mode on your file.

file = open('ycfile', 'a')

Now, whenever you call write(), it will just add the new text to the file, instead of overwriting it.
I have included this so you understand what is going wrong, and so that you know how to fix it if you come across it in the future. However, there is a better way of solving this.

Number 2, your code.
Instead of messing around with the file line by line, what you want to do is take all the file's text, change the bits you want, then replace the file text with this new text.
Perhaps it would look something like this:

def upgradeTrn():

    readfile = open('ycfile.txt', 'r+')
    text = readfile.read()
    lines = text.split('\n') # split the file content by line

    data = lines[10] #target the desired line
    trn = str(int(data) + 1)
    lines[10] = trn #replace the line with the new content

    new_text = '\n'.join(lines)
    readfile.write(new_text)
    readfile.close()

Read up more about append mode here , in case you're interested

Community
  • 1
  • 1
EriktheRed
  • 584
  • 1
  • 6
  • 16
  • Thanks for your advice ive made the code shorter now and its working and i did have \n after each bit i was writing but that left blank lines so i took it out and for some reason it puts each one on a new line which is what i wanted – maxpepsi May 01 '17 at 16:15