0

i want to write to text file without closing because i don't know what i'll stop close,i'll explain the hole issue

i've create text called resume.txt , so after each specific processes in my project it will overwrite in resume.txt so every time my project start it will check that file to know the last processes so my issue after each writing i have to close to apply it and i don't really think this is good i think there is better solution

this code will not work

wr = open('resume.txt','w')
login(usr,pas)
wr.write('login')
post(msg,con)
wr.write('post')
..so on 

the issue is how to write without closing , i can't write the wr.close at the end because it might be terminated by the user or connection time out .. etc

Hamoudaq
  • 1,490
  • 4
  • 23
  • 42
  • What are you trying to do exactly? The `with` keyword is probably what you're looking for. – Joel Cornett Aug 23 '12 at 19:33
  • Perhaps you want to flush the buffer to your file? Have a look at [this][1]. [1]: http://stackoverflow.com/questions/3167494/how-often-does-python-flush-to-a-file – Pascal Aug 23 '12 at 19:35
  • 3
    if you want to 'see' your writes in the file as you write them, you need to `flush()` the file handle after each write. this flushing of the write buffer is what is done on each file handle `close()` – tMC Aug 23 '12 at 19:42
  • tMC nice solution but it's not overwriting – Hamoudaq Aug 23 '12 at 21:54

3 Answers3

6

Not sure if this applies to your code, but what about wrapping in a with block?

with open('resume.txt','w') as wr:
    login(usr,pas)
    wr.write('login')
    # This is hacky, but it will go to the beginning 
    # of the file and then erase (truncate) it
    wr.seek(0)
    # I think you wanted to do this after you tried an action, 
    # but you can move it to wherever you want
    post(msg,con)
    wr.truncate()
    wr.write('post')

This will ensure that the file is closed on error. When you want to close the file, just start your next code on the same level as with with:

with open('resume.txt','w') as wr:
    login(usr,pas)
    wr.write('login')
    wr.seek(0)
    post(msg,con)
    wr.truncate()
    wr.write('post')
    # wr.seek(0) ...

# Next steps...

I would also recommend checking out the logging module to see if that can accomplish what you want.

RocketDonkey
  • 36,383
  • 7
  • 80
  • 84
  • Updating now - it is a bit/very hacky, but after each action it will reset the position to the beginning of the file and then remove the rest of the file. I don't know what your use case is, but you could also check out the logging (http://docs.python.org/library/logging.html) module, which can also handle writing to files. – RocketDonkey Aug 23 '12 at 22:06
3

first of all i'd like to thank tMC

the solution is

wr = open('resume.txt','w')
login(usr,pas)
wr.write('login')
wr.flush()
post(msg,con)
wr.seek(0)
wr.write('post')
wr.flush()

i've used flush() to write and apply and seek(0) for overwriting

Hamoudaq
  • 1,490
  • 4
  • 23
  • 42
-1

Try the with statement. A little comlicated to understand, but should do exactly this.

unddoch
  • 5,790
  • 1
  • 24
  • 37