tags:

views:

106

answers:

3

i am opening a csv file:

def get_file(start_file): #opens original file, reads it to array
  with open(start_file,'rb') as f:
    data=list(csv.reader(f))
    header=data[0]
    counter=collections.defaultdict(int)
    for row in data:
      counter[row[10]]+=1
  return (data,counter,header)

does the file stay in memory if i quit the program inside the WITH loop?

what happens to the variables in general inside the program when i quit the program without setting all variables to NULL?

+6  A: 

The operating system will automatically close any open file descriptors when your process terminates.

File data stored in memory (e.g. variables, Python buffers) will be lost. Data buffered in the operating system may be flushed to disk when the file is implicitly closed (checking the exact semantics of in-kernel dirty-buffers here would be educational, though you should not rely on it).

Your variables cease to exist when your process terminates.

Noah Watkins
To elaborate further, when you start a Python "program", a process is allocated by the Operating System for that Python "program" to be launched by `python.exe`. This process allocates memory and file descriptors for your "variables". When you "exit" the program, this process is destroyed--along with its allocated resources--by the OS itself.
Santa
Yeh, it's a little more difficult to explain the inter-workings for the Python virtual machine, as it relates to what people naturally assume to be their program (i.e. Python scripts).
Noah Watkins
+2  A: 

you never have to set variables to NULL, as soon as your program terminates the memory is freed. the same holds true for the file - it stays in memory no more or less whether you quit in the with loop or anywhere else. however, it is good practice to manually close the file so you can be sure that any pending operations are performed before the program is exited. in general, this should happen anyway, but especially when writing, I generally prefer the close.

Nicolas78
+4  A: 

My understanding of the with statement is that, no matter what, it will take care of closing your file handles for you when you exit it's scope. That should still happen if your program exits inside the with block.

As far as other variables are concerned, they're deleted from memory when your program exits automatically. If you are interested in finding ways to make something persistent between runs you can look at the pickle (http://docs.python.org/library/pickle.html) or shelve (http://docs.python.org/library/shelve.html) modules. Personally, I prefer shelve to pickle, but they both work well for that.

@gotgenes - Thanks for the suggestion. It's important to note that shelve uses pickle in its underlying implementation. When I say I prefer shelve to pickle, I mean that for the ways that persistence is important in what I'm currently designing using shelve is easier because it's not doing anything more than serving as a dictionary that persists between runs.

g.d.d.c
Links to pickle http://docs.python.org/library/pickle.html and shelve http://docs.python.org/library/shelve.html (please add them to this answer and delete this comment).
gotgenes
I think you should note that shelve uses pickle for object serialization. They are not two different methods for serialization, as your statement of preference suggests.
gotgenes