@stephanelsmith

On unix port, I'type of sys.stdin is a TextIOWrapper.

This is the correct behavior. However what's missing is that it should have a .buffer attribute to access the underlying binary stream -- this is the CPython-compatible way to do this (see the note at the end of the sys.stdin docs -- https://docs.python.org/3/library/sys.html#sys.stderr ).

Conversely, on ESP32, sys.stdin is a FileIO with sys.stdin.buffer available for use.

Confusingly, despite being named FileIO, this object (on all the embedded ports) is actually a TextIOWrapper-like implementation. It is a text stream, and supports the .buffer attribute to get the underlying binary stream.

I don't know the history, but I'm guessing it's called "FileIO" just to save the additional code size for "TextIOWrapper", but if you look at shared/runtime/sys_stdio_mphal.c you can see it's implemented with is_text set to true.

Why not FileIO which would provide text and buffered versions?

I think the solution is just to provide the buffer attribute on the unix version (in vfs_posix_file.c), which should be do-able using basically the same approach as sys_stdio_mphal.c.

🌐
MicroPython
docs.micropython.org › en › v1.5.1 › pyboard › library › sys.html
sys – system specific functions — MicroPython 1.5.1 documentation
sys.stdin¶ · standard input (connected to USB VCP, and optional UART object) sys.stdout¶ · standard output (connected to USB VCP, and optional UART object) sys.version¶ · Python language version that this implementation conforms to, as a string · sys.version_info¶ ·
🌐
MicroPython
docs.micropython.org › en › latest › library › sys.html
sys – system specific functions — MicroPython latest documentation
sys.stdin · Standard input stream. sys.stdout · Standard output stream. sys.tracebacklimit · A mutable attribute holding an integer value which is the maximum number of traceback entries to store in an exception. Set to 0 to disable adding tracebacks.
Discussions

select.poll not working properly with sys.stdin
sys.stdin.read() should immediately return when sending data to the serial interface on the PC the Pico is connected to. More on github.com
🌐 github.com
1
January 8, 2025
Binary over sys.stdin question, sys.stdin is type TextIOWrapper, not FileIO?
Hello all, Brief, I'm writing a CLI tool with micropython where I need to pipe in binary over stdin. On unix port, I'type of sys.stdin is a TextIOWrapper. Conversely, on ESP32, sys.stdin is a FileI... More on github.com
🌐 github.com
2
1
July 23, 2023
Data loss in string transfer through sys.stdout.write
Env: MicroPython v1.20.0 on 2023-04-26; Raspberry Pi Pico with RP2040 I have noticed constant data loss when transfer string objects with help of sys.stdout.write() method. MCU code: import sys BUF... More on github.com
🌐 github.com
7
July 18, 2023
Micropython automatically converting stdin carriage returns to newlines?
I was looking to modify some terminal code (specifically, aiorepl)to make it work nicely with windows clients and encountered this issue. When I press enter in a windows terminal, MicroPython (v1.1... More on github.com
🌐 github.com
1
1
October 27, 2022
🌐
Digi
docs.digi.com › resources › documentation › digidocs › 90002219 › reference › r_uart_constants.htm
How to use the primary UART
December 8, 2022 - MicroPython provides access to the primary UART via sys.stdin (see sys.stdin limitations) and sys.stdout (and sys.stderr as an alias to sys.stdout).
🌐
GitHub
github.com › micropython › micropython › issues › 16550
select.poll not working properly with sys.stdin · Issue #16550 · micropython/micropython
January 8, 2025 - import sys, select p = select.poll() p.register(sys.stdin, select.POLLIN) while True: for o, _ in p.ipoll(): if o is sys.stdin: read = sys.stdin.read() print('read', read)
Author   cxxcoder
🌐
MicroPython
docs.micropython.org › en › v1.17 › library › sys.html
sys – system specific functions — MicroPython 1.17 documentation
sys.stdin¶ · Standard input stream. sys.stdout¶ · Standard output stream. sys.version¶ · Python language version that this implementation conforms to, as a string. sys.version_info¶ · Python language version that this implementation conforms to, as a tuple of ints.
🌐
GitHub
github.com › orgs › micropython › discussions › 11448
Non blocking read of input from the terminal · micropython · Discussion #11448
May 9, 2023 - sys.stdin.read(1) will read that byte and block (potentially forever) on waiting for the next byte, because b'\xd0' is a start of an utf-8 character, and it was instructed to read 1 unicode char and return it as str, not to read 1 byte.
🌐
GitHub
github.com › micropython › micropython › issues › 12037
Data loss in string transfer through sys.stdout.write · Issue #12037 · micropython/micropython
July 18, 2023 - import sys BUF_SIZE = 40000 while True: sys.stdin.readline() line = bytes(BUF_SIZE) sys.stdout.write(line)
Author   pchala
Find elsewhere
🌐
MicroPython
docs.micropython.org › en › v1.9.3 › wipy › library › sys.html
sys – system specific functions — MicroPython 1.9.3 documentation
sys.stdin¶ · Standard input stream. sys.stdout¶ · Standard output stream. sys.version¶ · Python language version that this implementation conforms to, as a string. sys.version_info¶ · Python language version that this implementation conforms to, as a tuple of ints.
🌐
Readthedocs
micropython.readthedocs.io › en › v1.6 › library › sys.html
sys – system specific functions — MicroPython 1.6 documentation
The platform that MicroPython is running on. This is “pyboard” on the pyboard and provides a robust way of determining if a script is running on the pyboard or not. sys.stderr¶ · standard error (connected to USB VCP, and optional UART object) sys.stdin¶ ·
🌐
MicroPython
docs.micropython.org › en › v1.9 › esp8266 › library › sys.html
sys – system specific functions — MicroPython 1.9 documentation
sys.stdin¶ · Standard input stream. sys.stdout¶ · Standard output stream. sys.version¶ · Python language version that this implementation conforms to, as a string. sys.version_info¶ · Python language version that this implementation conforms to, as a tuple of ints.
🌐
MicroPython
docs.micropython.org › en › latest › genrst › modules.html
Modules — MicroPython latest documentation
import sys sys.stdin = None print(sys.stdin) Versions and Downloads latest · Versions · v1.4.4 · v1.4.5 · v1.4.6 · v1.5 · v1.5.1 · v1.5.2 · v1.6 · v1.7 · v1.8 · v1.8.1 · v1.8.2 · v1.8.3 · v1.8.4 · v1.8.5 · v1.8.6 · v1.8.7 · v1.9 · v1.9.1 · v1.9.2 ·
🌐
GitHub
github.com › micropython › micropython › issues › 3356
[baremetal] sys.stdin does not handle ctrl-D EOF markers · Issue #3356 · micropython/micropython
October 7, 2017 - sys.stdin.read(), sys.stdin.readlines(), etc. do not handle ctrl-D at the beginning of a line as EOF (except on ports like unix). So for instance, there's no way to terminate input when doing sys.s...
Author   dhalbert
🌐
GitHub
github.com › micropython › micropython › issues › 1293
esp8266: can't read from sys.stdin · Issue #1293 · micropython/micropython
May 28, 2015 - I was trying to work around the fact that the ESP8266 port doesn't have UART support on the Python side yet, and simply read/write from and to the sys.stdin and sys.stdout. While sys.stdout works as expected, trying to read even a single...
Published   May 28, 2015
Author   deshipu
🌐
Pycom User Forum
forum.pycom.io › home › getting started › micropython › adding timeout to sys.stdin.read(1)
Adding timeout to sys.stdin.read(1) | Pycom user forum
February 21, 2022 - I am trying to read from the serial and telnet using sys.stdin.read(1). Since the function is blocking, I tried some suggested options with uselect: import sys, uselect spoll=uselect.poll() spoll.register(sys.stdin, uselect.POLLIN) def read1(): retu...
Top answer
1 of 6
49

So you have used Python's "pre built in functions", presumably like this:

file_object = open('filename')
for something in file_object:
    some stuff here

This reads the file by invoking an iterator on the file object which happens to return the next line from the file.

You could instead use:

file_object = open('filename')
lines = file_object.readlines()

which reads the lines from the current file position into a list.

Now, sys.stdin is just another file object, which happens to be opened by Python before your program starts. What you do with that file object is up to you, but it is not really any different to any other file object, its just that you don't need an open.

for something in sys.stdin:
    some stuff here

will iterate through standard input until end-of-file is reached. And so will this:

lines = sys.stdin.readlines()

Your first question is really about different ways of using a file object.

Second, where is it reading from? It is reading from file descriptor 0 (zero). On Windows it is file handle 0 (zero). File descriptor/handle 0 is connected to the console or tty by default, so in effect it is reading from the keyboard. However it can be redirected, often by a shell (like bash or cmd.exe) using syntax like this:

myprog.py < input_file.txt 

That alters file descriptor zero to read a file instead of the keyboard. On UNIX or Linux this uses the underlying call dup2(). Read your shell documentation for more information about redirection (or maybe man dup2 if you are brave).

2 of 6
8

It is reading from the standard input - and it should be provided by the keyboard in the form of stream data.

It is not required to provide a file, however you can use redirection to use a file as standard input.

In Python, the readlines() method reads the entire stream, and then splits it up at the newline character and creates a list of each line.

lines = sys.stdin.readlines()

The above creates a list called lines, where each element will be a line (as determined by the end of line character).

You can read more about this at the input and output section of the Python tutorial.

If you want to prompt the user for input, use the input() method (in Python 2, use raw_input()):

user_input = input('Please enter something: ')
print('You entered: {}'.format(user_input))
🌐
MicroPython
docs.micropython.org › en › v1.9.4 › esp8266 › library › sys.html
sys – system specific functions — MicroPython 1.9.4 documentation
sys.stdin¶ · Standard input stream. sys.stdout¶ · Standard output stream. sys.version¶ · Python language version that this implementation conforms to, as a string. sys.version_info¶ · Python language version that this implementation conforms to, as a tuple of ints.
🌐
GitHub
github.com › orgs › micropython › discussions › 9954
How to override stdin for tests simulating user input? · micropython · Discussion #9954
November 14, 2022 - @DavesCodeMusings I think you can use os.dupterm to achieve this in MicroPython (this is how things like WebREPL inject stdin/stdout).