Have to close file before reading it? Python
<p>This program simply takes a file, deletes it, allows the user to input two lines which are put into the blank file, then prints the file. </p>

<p>But why do I have to close the file object and reopen it before it displays the newly added lines? </p>

<p>(Notice the file isn't printed in this version of the code. But if you remove the #'s it will perform correctly)</p>

<pre><code>from sys import argv

sript, filename = argv

print "We will be buliding a new file from an %s" % filename
print "If you don't want to do this, hit CTRL_C"
print "If you do, hit and other key"

raw_input("&gt;&gt;&gt;")

print "Oppening the file..."
file_ob = open(filename, "r+")
file_ob.truncate()

print "Now we will rewrite this file with the following line"
line1 = raw_input("The fist line will be :")
line2 = raw_input("The second line will be:")

print "Now we're put them into the file"
file_ob.write("\n\t&gt;&gt;&gt;" + line1 + "\n\n\t&gt;&gt;&gt;" + line2)

print "And now we will see what is in the file we just made"
print file_ob.read()

file_ob.close()

print "And now we will see what is in the file we just made"


#file_ob = open(filename, "r+")
#print file_ob.read()
#file_ob.close()
</code></pre>