0

I'd like to use 2 stream pointers within a stream, and position the 2 pointers at different positions. How do I make a copy of the first stream, so that the copy doesn't mirror the state of the first stream, from this point in time?

In particular, I'm interested in streams of the type io.BytesIO()


import io

stream1 = open("Input.jpg", "rb")

stream2 = stream1

print('A', stream1.tell(), stream2.tell())

stream1.seek(10)

print('B', stream1.tell(), stream2.tell())

My goal is to see output of

A 0 0
B 10 0

However, I see

A 0 0
B 10 10

@varela Thanks for the response. Unfortunately, this doesn't work well when the stream doesn't have a file descriptor (which can happen if we don't open a file). For example, instead of stream1=open("Input.jpg", "rb")

stream1 = io.BytesIO() image.save(stream1, format='JPEG')

Any suggestions on how to handle this case?

Thanks.

1 Answers1

0

You can open file twice, like

stream1 = open("Input.jpg", "rb")
stream2 = open("Input.jpg", "rb")

Then they will be independent. When you do stream2 = stream1 you just copy object reference, which doesn't create any new object. You need to remember to close both file objects as well.

Usually copy of file descriptions is not needed. However it's possible to do with low level system operations, but I wouldn't recommend to do it unless you really have use case for this, example:

import os

# return integer file handle
fd1 = os.open("Input.jpg", os.O_BINARY | os.O_RDONLY)
fd2 = os.dup(fd1)

# you can convert them to file objects if required.
stream1 = os.fdopen(fd1, 'rb')
stream2 = os.fdopen(fd2, 'rb')

Here some use cases when os.dup makes sense to use: dup2 / dup - why would I need to duplicate a file descriptor?

varela
  • 1,281
  • 1
  • 10
  • 16