RE: Need a little parse help

Discussion in 'Python' started by Delaney, Timothy C (Timothy), May 11, 2005.

  1. Peter Hansen wrote:

    > In my opinion, if the code fits on one screen and just reads stuff
    > from one file and, maybe, writes to another, you can safely and with

    ^^^^^^
    > clean conscience ignore Mike's advice (but remember it for later!).


    Remember, finalisers are not called when Python exits. So if you don't
    explicitly close the file you are *writing* to, it may not be flushed
    before being closed (by the OS because the process no longer exists).

    Tim Delaney
    Delaney, Timothy C (Timothy), May 11, 2005
    #1
    1. Advertising

  2. Delaney, Timothy C (Timothy)

    Peter Hansen Guest

    Delaney, Timothy C (Timothy) wrote:
    > Peter Hansen wrote:
    >>In my opinion, if the code fits on one screen and just reads stuff
    >>from one file and, maybe, writes to another, you can safely and with

    > ^^^^^^
    >>clean conscience ignore Mike's advice (but remember it for later!).

    >
    > Remember, finalisers are not called when Python exits. So if you don't
    > explicitly close the file you are *writing* to, it may not be flushed
    > before being closed (by the OS because the process no longer exists).


    Ouch... I'd forgotten/never heard that I guess. If that's true and all
    there is to the matter, I would think we'd all have encountered numerous
    times when our output files were not properly written. Yet I've never
    encountered this situation myself, nor heard of anyone else ever having
    trouble as a result of it. What gives?

    -Peter
    Peter Hansen, May 11, 2005
    #2
    1. Advertising

  3. Peter Hansen wrote:

    > > Remember, finalisers are not called when Python exits. So if you don't
    > > explicitly close the file you are *writing* to, it may not be flushed
    > > before being closed (by the OS because the process no longer exists).

    >
    > Ouch... I'd forgotten/never heard that I guess. If that's true and all
    > there is to the matter, I would think we'd all have encountered numerous
    > times when our output files were not properly written. Yet I've never
    > encountered this situation myself, nor heard of anyone else ever having
    > trouble as a result of it. What gives?


    that's probably because finalizers *are* called when Python exits.

    $ python2.4
    >>> f = open("out", "w")
    >>> f.write(100000*"x")
    >>> import os
    >>> os._exit(1)

    $ ls -l out
    -rw-rw-r-- 1 fredrik fredrik 98304 May 11 20:18 out
    $ rm out
    $ python2.4
    >>> f = open("out", "w")
    >>> f.write(100000*"x")
    >>> ^D

    $ ls -l out
    -rw-rw-r-- 1 fredrik fredrik 100000 May 11 20:19 out

    </F>
    Fredrik Lundh, May 11, 2005
    #3
  4. Delaney, Timothy C (Timothy)

    Andrew Dalke Guest

    Delaney, Timothy C (Timothy) wrote:
    > Remember, finalisers are not called when Python exits. So if you don't
    > explicitly close the file you are *writing* to, it may not be flushed
    > before being closed (by the OS because the process no longer exists).


    Wrong.

    % python
    Python 2.3 (#1, Sep 13 2003, 00:49:11)
    [GCC 3.3 20030304 (Apple Computer, Inc. build 1495)] on darwin
    Type "help", "copyright", "credits" or "license" for more information.
    >>> class ABC:

    .... def __del__(self):
    .... print "I am here!"
    ....
    >>> abc = ABC()
    >>> del abc

    I am here!
    >>> abc = ABC()
    >>> import sys
    >>> sys.exit()

    I am here!
    %

    There's documentation somewhere that describes what occurs
    during Python's exit, but I can't find it right now.

    Andrew
    Andrew Dalke, May 11, 2005
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. selekta
    Replies:
    7
    Views:
    348
    CBFalconer
    Jun 2, 2004
  2. Alex Nordhus

    Need a little parse help

    Alex Nordhus, May 10, 2005, in forum: Python
    Replies:
    9
    Views:
    301
    James Stroud
    May 11, 2005
  3. Replies:
    19
    Views:
    1,119
    Daniel Vallstrom
    Mar 15, 2005
  4. ThaDoctor
    Replies:
    3
    Views:
    379
    Alan Woodland
    Sep 28, 2007
  5. Daniel
    Replies:
    1
    Views:
    208
    Bart van Ingen Schenau
    Jul 9, 2013
Loading...

Share This Page