2

What I'm doing is pretty basic, but for some reason isn't actually writing anything into the text file I need.

The first thing I've done is gotten input from the user and assigned it to assoc. This works fine, as I can print out assoc whenever I please, and that appears to work completely fine.

Next, I open a different file depending on whether or not assoc is equal to 0, 1, or 2. I read all the lines and assign the list of read lines to the variable beta, then I grab the length of beta and assign it to prodlen, add one two prodlen and assign that new value to localid and close the object. The only reason I'm including this is because I fear I've missed something crucial and simple.

if assoc==0:
    fob=open('pathto/textfile1.txt','r')
if assoc==1:
    fob=open('pathto/textfile2.txt','r')
if assoc==2:
    fob=open('pathto/textfile3.txt','r')

beta=fob.readlines();
prodlen=len(beta);
localid=prodlen+1;
fob.close;

After I get the user input, open the file, list its contents, and read its length, I then use the user's input again to open the file with writing permissions. (I've only included one of the possible if statements, because the others are identical except for which file they write to and what VALUE, which is a string, is). I append list beta with \n to get a line break, followed by a string, which has been represented here by VALUE. I then add localid onto the end, in string form.

if assoc==0:
    fob=open('pathto/textfile1.txt','w')
    beta.append("\nVALUE"+str(localid))
    print (beta)
    fob.writelines(beta)

My true problem, though, is in the last two lines. When I print out list beta, it includes the new value that I've appended. But when I try to write the list to the file, it clears any data that was currently in the file and doesn't write anything inside! I'm a python noob, so please, keep the solution simple (if possible). I assume the solution to this is relatively simple. I'm probably just overlooking something.

Ian Zane
  • 2,209
  • 5
  • 23
  • 21

1 Answers1

2

use the 'a' option instead of 'w' in your open call. w overwrites, a appends.

http://docs.python.org/2/library/functions.html#open

python open built-in function: difference between modes a, a+, w, w+, and r+?

is a useful explanation of different modes.

Community
  • 1
  • 1
Colleen
  • 23,899
  • 12
  • 45
  • 75
  • Will I still be able to use writelines? Or is there a different function for writing to a file using `'a'`? – Ian Zane Feb 20 '13 at 00:46
  • you should still be able to use writelines-- it's just opening the file in a different mode. – Colleen Feb 20 '13 at 00:47
  • Looks as though this _should_ work, but when I try `fob=open('pathto/textfile1.txt','a+')` followed by `fob.writelines("\nVALUE"+str(localid))`, then, `fob.close`, it still doesn't write. – Ian Zane Feb 20 '13 at 00:52
  • yeah, I was wondering about that. Seems like you should have been able to write *something* in the first place. What version of python are you using? – Colleen Feb 20 '13 at 00:54
  • ok, I was wondering if it was deprecated but it isn't. Wondering now if having the \n at the beginning is messing you up somehow? Seems like it shouldn't.... – Colleen Feb 20 '13 at 00:57
  • It doesn't wipe the file anymore though. It keeps the earlier lines I had. – Ian Zane Feb 20 '13 at 00:57
  • do you explicitly close the file? It may be that writes are not saved without an explicit close. – Colleen Feb 20 '13 at 00:58
  • What's the difference between a regular close and an explicit close? I'm just using fob.close (closes the file object). – Ian Zane Feb 20 '13 at 00:59
  • that's what I meant (as opposed to just exiting the method or something that might implicitly close it). `close()`, right, not just `close` ? – Colleen Feb 20 '13 at 01:00
  • 1
    Forgot parentheses. My bad. It works now. Edit: Thank you! – Ian Zane Feb 20 '13 at 01:01
  • lol no worries, happens to everyone. And upon reading the docs on `write()`, yes, you must explicitly call `close()` or `flush()` to save the changes. – Colleen Feb 20 '13 at 01:02