Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6966461
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T16:11:41+00:00 2026-05-27T16:11:41+00:00

I have a class which extends BufferedReader, and a list of file streams b.close()

  • 0

I have a class which extends BufferedReader, and a list of file streams
b.close() is called for all except the last stream, I want to keep the streams open
How do I do this?

thanks


class TestReader(BufferedReader):
    pass

def test(streams):
    for stream in streams:
        b=TestReader(stream)
        do_something(b)
    #all the streams except streams[-1] are closed, how do I prevent this?

streams=[open('test1.txt','rb'),open('test2.txt','rb')]
test(streams)
streams.do_something_else()

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T16:11:42+00:00Added an answer on May 27, 2026 at 4:11 pm

    Even though in the implementation BufferedIOBase classes wrap an IOBase object, their interface is a stream (everything inherits from IOBase), so the ordinary behavior of a IOBase object is to close themselves when they go out of scope. BufferedIOBase just delegates the close() call to the underlying stream.

    You shouldn’t view a BufferedReader as a stream wrapper (although that is how it is implemented), but as a type-casting of an existing stream. The state of the two streams is completely bound together. However, you can unbind the wrapped stream with detach(), but this leaves the BufferedIOBase object useless.

    Additionally, io.open returns a BufferedReader already when mode is rb, so you are double-buffering. You should use io.FileIO instead.

    You have a few choices:

    1. Create a new stream and a new underlying file descriptor, and pass around file names instead of streams. This is your easiest and safest option.

    2. Create raw file descriptors and create streams from them as needed. This requires some
      care that multiple streams are not using the same file descriptor at the same time. For example:

      fd = os.open('test.txt', os.O_RDONLY)
      file1 = FileIO(fd, 'r', closefd=False)
      file2 = FileIO(fd, 'r', closefd=False)
      
      file1.read(100)
      assert file1.tell() == 100
      file2.read(100)
      assert file1.tell() == 200
      
    3. detach() the underlying stream before your BufferedIOBase object closes its stream. (Remember to rewind the stream!)

      def test(streams):
          for stream in streams:
              b=TestReader(stream)
              do_something(b)
              wrappedstream = b.detach()
              assert wrappedstream is stream
      

      You can even implement this in your destructor:

      class TestReader(BufferedReader):
          def __del__(self):
              self.detach()
              # self.raw will not be closed,
              # rather left in the state it was in at detachment
      

      Or just disable close() delegation completely if you think the semantics are wrong:

      class TestReader(BufferedReader):
          def close(self):
              self.closed = True
      

    I don’t have the big picture of what you are doing (possibly you need a different design), but this is how I would implement the code I see:

    from io import FileIO, BufferedReader
    import io
    import os
    
    class TestReader(BufferedReader):
        pass
    
    def test(streams):
        for stream in streams:
            b = TestReader(stream)
    
    def test_reset(streams):
        """Will try to leave stream state unchanged"""
        for stream in streams:
            pos = stream.tell()
            b = TestReader(stream)
            do_something(b)
            b.detach()
            stream.seek(pos)
    
    
    
    filenames = ['test1.txt', 'test2.txt']
    
    # option 1: just make new streams
    
    streams = [FileIO(name, 'r') for name in filenames]
    test(streams)
    streams = [io.open(name, 'rb') for name in filenames]
    #etc
    
    
    # option 2: use file descriptors
    fds = [os.open(name, os.O_RDONLY) for name in filenames]
    #closefd = False means "do not close fd on __del__ or __exit__"
    #this is only an option when you pass a fd instead of a file name
    streams = [FileIO(fd, 'r', closefd=False) for fd in fds]
    test(streams)
    streams = []
    for fd in fds:
        os.lseek(fd, 0, os.SEEK_SET)
        streams.append(io.open(fd, 'rb', closefd=False))
        # you can also .seek(0) on the BufferedReader objects
        # instead of os.lseek on the fds
    
    
    # option 3: detach
    
    streams = [FileIO(name, 'r') for name in filenames]
    test_reset(streams)
    # streams[*] should still be in the same state as when you passed it in
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a class called Node and another class called ClassicNode which extends Node.
my question is simple. I have class TouchSurfaceView which extends GLSurfaceView. I want create
I have a class called 'Items' to which 'Equips' extends from and 'Helmet' then
I have a class which extends ListActivity within I have all methods for managing
I have 3 class file, when i compile the class which extends the other
I have a custom class loader which extends from a URLClassLoader. I added a
I have a view in Eclipse (implemented by a class which extends org.eclipse.ui.part.ViewPart )
I have 2 web user controls, both inherit the same base class which extends
I have two Java classes: B, which extends another class A, as follows :
I have a static class in a shared project, which I want to extend

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.