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. Advertisements

  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. Advertisements

  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. Advertisements

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,978
  2. damezumari
    Replies:
    2
    Views:
    644
    damezumari
    Apr 27, 2006
  3. j vickroy
    Replies:
    5
    Views:
    1,042
    Peter Otten
    Nov 14, 2003
  4. Marco Nicosia
    Replies:
    0
    Views:
    533
    Marco Nicosia
    Feb 25, 2005
  5. Replies:
    4
    Views:
    533
    Michael Wojcik
    Apr 28, 2005
  6. ChrisKaelin
    Replies:
    2
    Views:
    253
    ChrisKaelin
    Apr 17, 2007
  7. Matt Kruse
    Replies:
    5
    Views:
    577
    Richard Cornford
    Sep 9, 2003
  8. Brad Baxter

    do 'file' -- when is file closed?

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