file object: seek and close?

Shu-Hsien Sheu sheu at bu.edu
Thu Sep 18 19:41:24 CEST 2003


Hi,

I did some tests and it seems that it was the line that I defined list 
as a pdbdict0 object was causing the problem.

If I remove the line:

list = pdbdict0()

there would be no IOError messages.

I tried defining list  as a dictionary, and there was IOError as well.
However, if I defined list as a list, or defined any variable other than 
'list' as dict, it would be just fine. Really weird.

If I remove the seek method, there would be no IOError message either.

It seems that, f.seek plus ' list = somekind of dictionary' caused the 
file object to close.

Great thanks!

-shuhsien

----------------------------------------------------------------------------------------------------------------------------

class pdbdict0(dict):
    def __init__(self):
        super(dict, self).__init__()
        self['header'] = ''
        self['compnd'] = []

# read the pdb file
f = open('Z:\\3.4.21.1\\1acb.pdb', 'r')
             
# check if the file is an old type pdb (type = 0) or a new type pdb 
(type = 1)
type = 0
for lines in f:
    if lines[:6] == 'TITLE':
        type = 1
    if lines[:6] == 'COMPND':
        break
   
f.seek(0)


# read in the information

if type == 0:
    list = pdbdict0()      # << this line seems to be causing the file 
object to close
    for lines in f:
        lines = lines[:70]
..........................................................................

Traceback (most recent call last):
  File "C:\Documents and Settings\sheu\My 
Documents\precise\parspdbtest0.py", line 27, in -toplevel-
    for lines in f:
ValueError: I/O operation on closed file
----------------------------------------------------------------------------------------------------------------------------------------------






More information about the Python-list mailing list