1

Following is my code

#!/usr/bin/python
from sys import argv
x, file_name = argv
print "planning to erase file %r. " , file_name
tgt=open(file_name,'r+')
print "deleting"
tgt.truncate()
print tgt.read()
print " no lines "
print "closed : ", tgt.closed
print "mode : ", tgt.mode
l1 = raw_input("line1 : ")
l2 = raw_input("line2 : ")
l3 = raw_input("line3 : ")
tgt.write(l1)
tgt.write('\n')
tgt.write(l2)
tgt.write('\n')
tgt.write(l3)
print "after writing"
print tgt.read()
print "Going to replace "
tgt.close()

Last read statement does not display data. But the file is updated with 3 lines I entered. Can any help

1 Answer 1

3

You need to read the file once again by moving the cursor to the top

tgt.seek(0)
print tgt.read()
Sign up to request clarification or add additional context in comments.

2 Comments

I believe on Windows systems you might also need to close the file first before the changes are added. I'm not sure though. Although that could only apply to actually opening the file to view it as opposed to directly accessing the file object.
You can call flush(), I think? It's buffering, python on Linux does it too.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.