Check if a file is closed

Discussion in 'Python' started by camillo@rockit.it, Jun 19, 2006.

  1. Guest

    How to check if a file is closed?

    On Win32 you can call CreateFile with write and share write and if it
    raises an error, the file is closed.

    How to do it in Python???

    Thanks,
    Camillo
    , Jun 19, 2006
    #1
    1. Advertising

  2. enlightened us with:
    > How to check if a file is closed?
    >
    > On Win32 you can call CreateFile with write and share write and if it
    > raises an error, the file is closed.


    On Linux you can read /proc to see which process has which file
    opened, and if it isn't in there, it's closed.

    Sybren
    --
    The problem with the world is stupidity. Not saying there should be a
    capital punishment for stupidity, but why don't we just take the
    safety labels off of everything and let the problem solve itself?
    Frank Zappa
    Sybren Stuvel, Jun 19, 2006
    #2
    1. Advertising

  3. wrote:
    > How to check if a file is closed?


    >>> f = open('trashme.txt', 'w')
    >>> f

    <open file 'trashme.txt', mode 'w' at 0x2aaaaab66e40>
    >>> dir(f)

    ['__class__', '__delattr__', '__doc__', '__getattribute__', '__hash__',
    '__init__', '__iter__', '__new__', '__reduce__', '__reduce_ex__',
    '__repr__', '__setattr__', '__str__', 'close', 'closed', 'encoding',
    'fileno', 'flush', 'isatty', 'mode', 'name', 'newlines', 'next', 'read',
    'readinto', 'readline', 'readlines', 'seek', 'softspace', 'tell',
    'truncate', 'write', 'writelines', 'xreadlines']
    >>> f.closed

    False
    >>> f.close()
    >>> f.closed

    True


    --
    bruno desthuilliers
    python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
    p in ''.split('@')])"
    bruno at modulix, Jun 19, 2006
    #3
    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. Replies:
    5
    Views:
    7,713
  2. Replies:
    4
    Views:
    433
    Michael Wojcik
    Apr 28, 2005
  3. ChrisKaelin
    Replies:
    2
    Views:
    117
    ChrisKaelin
    Apr 17, 2007
  4. Matt Kruse
    Replies:
    5
    Views:
    307
    Richard Cornford
    Sep 9, 2003
  5. Brad Baxter

    do 'file' -- when is file closed?

    Brad Baxter, Apr 5, 2006, in forum: Perl Misc
    Replies:
    7
    Views:
    100
    A. Sinan Unur
    Apr 5, 2006
Loading...

Share This Page