file object: seek and close?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Shu-Hsien Sheu

    file object: seek and close?

    Hi,

    Does the seek method would close the file object after using a for loop?
    My program looks like this:

    f = open('somefile' , 'r')
    for lines in f:
    some operations
    f.seek(0)

    for lines in f:
    other operations

    Python gave me an IOErros saying that the file object is already closed.
    I am now opening the file again under another file object.

    Am I doing something wrong or it's a bug?
    Thanks!

    -shuhsien


  • Peter Otten

    #2
    Re: file object: seek and close?

    Shu-Hsien Sheu wrote:
    [color=blue]
    > Does the seek method would close the file object after using a for loop?
    > My program looks like this:
    >
    > f = open('somefile' , 'r')
    > for lines in f:
    > some operations
    > f.seek(0)
    >
    > for lines in f:
    > other operations
    >
    > Python gave me an IOErros saying that the file object is already closed.
    > I am now opening the file again under another file object.
    >
    > Am I doing something wrong or it's a bug?[/color]

    The code you posted works here (I substituted "other operations" with "print
    lines"). Please reduce your actual code to the minimum that still fails and
    repost together with cut and pasted traceback.

    Peter

    Comment

    • Shu-Hsien Sheu

      #3
      Re: file object: seek and close?

      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.2 1.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:\Documen ts and Settings\sheu\M y
      Documents\preci se\parspdbtest0 .py", line 27, in -toplevel-
      for lines in f:
      ValueError: I/O operation on closed file
      ----------------------------------------------------------------------------------------------------------------------------------------------

      Comment

      • Shu-Hsien Sheu

        #4
        Re: file object: seek and close?

        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.2 1.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:\Documen ts and Settings\sheu\M y
        Documents\preci se\parspdbtest0 .py", line 27, in -toplevel-
        for lines in f:
        ValueError: I/O operation on closed file
        ----------------------------------------------------------------------------------------------------------------------------------------------


        Comment

        • Peter Otten

          #5
          Re: file object: seek and close?

          Shu-Hsien Sheu wrote:
          [color=blue]
          > 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()
          >[/color]

          This is really strange. I still cannot reproduce the error.
          [color=blue]
          >
          > class pdbdict0(dict):
          > def __init__(self):
          > super(dict, self).__init__( )
          > self['header'] = ''
          > self['compnd'] = []
          >[/color]

          Wouldn't that be:

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

          or better:

          def bdbdict0():
          return {"header": "", "compnd": []}

          While the pdbdict0 implementation seems broken, I have no clue what causes
          the file to be closed.

          Totally unrelated: Don't use list and type as variable names. They are
          already used as built-in classes.

          Peter

          Comment

          • John Roth

            #6
            Re: file object: seek and close?


            "Peter Otten" <__peter__@web. de> wrote in message
            news:bkctm3$34s $05$1@news.t-online.com...[color=blue]
            > Shu-Hsien Sheu wrote:
            >[color=green]
            > > 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()
            > >[/color]
            >
            > This is really strange. I still cannot reproduce the error.
            >[color=green]
            > >
            > > class pdbdict0(dict):
            > > def __init__(self):
            > > super(dict, self).__init__( )
            > > self['header'] = ''
            > > self['compnd'] = []
            > >[/color]
            >
            > Wouldn't that be:
            >
            > class pdbdict0(dict):
            > def __init__(self):
            > super(pdbdict0, self).__init__( self)
            > self['header'] = ''
            > self['compnd'] = []
            >
            > or better:
            >
            > def bdbdict0():
            > return {"header": "", "compnd": []}
            >
            > While the pdbdict0 implementation seems broken, I have no clue what causes
            > the file to be closed.
            >
            > Totally unrelated: Don't use list and type as variable names. They are
            > already used as built-in classes.[/color]

            Actually, that's probably the problem right there. Never, ever use anything
            in the built-in namespace as a variable name.

            John Roth
            [color=blue]
            >
            > Peter[/color]


            Comment

            Working...