Check if a file is closed

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • camillo@rockit.it

    #1

    Check if a file is closed

    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

  • bruno at modulix

    #2
    Re: Check if a file is closed

    camillo@rockit. it wrote:[color=blue]
    > How to check if a file is closed?[/color]
    [color=blue][color=green][color=darkred]
    >>> f = open('trashme.t xt', 'w')
    >>> f[/color][/color][/color]
    <open file 'trashme.txt', mode 'w' at 0x2aaaaab66e40>[color=blue][color=green][color=darkred]
    >>> dir(f)[/color][/color][/color]
    ['__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'][color=blue][color=green][color=darkred]
    >>> f.closed[/color][/color][/color]
    False[color=blue][color=green][color=darkred]
    >>> f.close()
    >>> f.closed[/color][/color][/color]
    True


    --
    bruno desthuilliers
    python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
    p in 'onurb@xiludom. gro'.split('@')])"

    Comment

    Working...