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

  • Home
  • SEARCH
  • 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 9122241
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T06:03:20+00:00 2026-06-17T06:03:20+00:00

i want to read bytes. sys.stdin is opened in textmode, yet it has a

  • 0

i want to read bytes. sys.stdin is opened in textmode, yet it has a buffer that can be used to read bytes: sys.stdin.buffer.

my problem is that when i pipe data into python i only seem to have 2 options if i want readahead, else i get a io.UnsupportedOperation: File or stream is not seekable.

  1. reading buffered text from sys.stdin, decoding that text to bytes, and seeking back

    (sys.stdin.read(1).decode(); sys.stdin.seek(-1, io.SEEK_CUR).

    unacceptable due to non-encodable bytes in the input stream.

  2. using peek to get some bytes from the stdin’s buffer, slicing that to the appropriate number, and praying, as peek doesn’t guarantee anything: it may give less or more than you request…

    (sys.stdin.buffer.peek(1)[:1])

    peek is really underdocumented and gives you a bunch of bytes that you have to performance-intensively slice.

btw. that error really only applies when piping: for ./myscript.py <somefile, sys.stdin.buffer supports seeking. yet the sys.stdin is always the same hierarchy of objects:

$ cat testio.py
#!/usr/bin/env python3
from sys import stdin
print(stdin)
print(stdin.buffer)
print(stdin.buffer.raw)"
$ ./testio.py
<_io.TextIOWrapper name='<stdin>' mode='r' encoding='UTF-8'>
<_io.BufferedReader name='<stdin>'>
<_io.FileIO name='<stdin>' mode='rb'>
$ ./testio.py <somefile
[the same as above]
$ echo hi | ./testio.py
[the same as above]

some initial ideas like wrapping the byte stream into a random access buffer fail with the same error as mentioned above: BufferedRandom(sys.stdin.buffer).seek(0) ⇒ io.UnsupportedOperation…

finally, for your convenience i present:

Python’s io class hierarchy

IOBase
├RawIOBase
│└FileIO
├BufferedIOBase  (buffers a RawIOBase)
│├BufferedWriter┐ 
│├BufferedReader│
││        └─────┴BufferedRWPair
│├BufferedRandom (implements seeking)
│└BytesIO        (wraps a bytes)
└TextIOBase
 ├TextIOWrapper  (wraps a BufferedIOBase)
 └TextIO         (wraps a str)

and in case you forgot the question: how do i get the next byte from stdin without de/encoding anything, and without advancing the stream’s cursor?

  • 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-06-17T06:03:22+00:00Added an answer on June 17, 2026 at 6:03 am

    The exception doesn’t come from Python, but from the operating system, which doesn’t allow seeking on pipes. (If you redirect output from a regular pipe, it can be seeked, even though it’s standard input.) This is why you get the error in one case and not in the other, even though the classes are the same.

    The classic Python 2 solution for readahead would be to wrap the stream in your own stream implementation that implements readahead:

    class Peeker(object):
        def __init__(self, fileobj):
            self.fileobj = fileobj
            self.buf = cStringIO.StringIO()
    
        def _append_to_buf(self, contents):
            oldpos = self.buf.tell()
            self.buf.seek(0, os.SEEK_END)
            self.buf.write(contents)
            self.buf.seek(oldpos)
    
        def peek(self, size):
            contents = self.fileobj.read(size)
            self._append_to_buf(contents)
            return contents
    
        def read(self, size=None):
            if size is None:
                return self.buf.read() + self.fileobj.read()
            contents = self.buf.read(size)
            if len(contents) < size:
                contents += self.fileobj.read(size - len(contents))
            return contents
    
        def readline(self):
            line = self.buf.readline()
            if not line.endswith('\n'):
                line += self.fileobj.readline()
            return line
    
    sys.stdin = Peeker(sys.stdin)
    

    In Python 3 supporting the full sys.stdin while peeking the undecoded stream is complicated—one would wrap stdin.buffer as shown above, then instantiate a new TextIOWrapper over your peekable stream, and install that TextIOWrapper as sys.stdin.

    However, since you only need to peek at sys.stdin.buffer, the above code will work just fine, after changing cStringIO.StringIO to io.BytesIO and '\n' to b'\n'.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a buffer with n bytes, but I only want read in sizeof(something)
I want to read a sequence of bytes from my accelerometer. I can't get
I want to be able to read bytes from a min and max hexadecimal
Hi I can't understand this error I want read this XML file: <?xml version=1.0
I have a text file. I want read that file. But In that if
I want to read 4 bytes which are a little-endian encoding of an unsigned
I want to read bytes from a wave file into an array. Since the
I want to read bytes from a file and then write those bytes to
I want to read bytes directly off a hard drive, preferably using python. How
After you establish a connection and you want to read the bytes coming from

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.