开发者

python: what happens to opened file if i quit before it is closed?

开发者 https://www.devze.com 2023-01-10 10:06 出处:网络
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:

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]
    cou开发者_开发知识库nter=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?


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.


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号