I have been following "Learn python the hard way" for learning python. The following script is directly taken from the Exercise 16 of the book:
#Exercise 16: Reading and Writing Files
from sys import argv
script, filename = argv
print "We're going to erase %r." %filename
print "If you don't want that, hit (^C)."
print "If you do want that, hit RETURN."
raw_input("?")
print "Opening the file..."
target = open(filename,'w')
print "Truncating the file. Goodbye!"
target.truncate()
print "Now I'm going to ask you for three lines."
line1 = raw_input("line1: ")
line2 = raw_input("line2: ")
line3 = raw_input("line3: ")
print "I'm going to write these to the file."
target.write(line1 + "\n" + line2 + "\n" + line3 + "\n")
Suppose the file storing the above script is 'ex1.py'.I decided to run the script file with the following command in the Ubuntu terminal
$ python ex1.py ex1.py
To my surprise, I was successful in writing to the same file storing the script. Why does this work? Is the script loaded into the terminal before interpretation starts?
P.S. I am mainly asking this question because it was my idea that python is interpreted and not compiled. So the object file for the code is never created. I thought that no compiled version of the script is ever created.
Edit: I think that the second part of the question is already discussed in this stackoverflow question.