What could cause `sys.stdin` to have to be opened twice to read input?

  bash, git-for-windows, python-3.x, windows

What possible things could cause sys.stdin to have to be opened twice to read input from a pipe?

Doesn’t work:

#!/usr/bin/env python3
import sys

stdin = sys.stdin.read()
for line in stdin.splitlines():
    print(line)

Works:

#!/usr/bin/env python3
import sys

stdin = sys.stdin.read()
stdin = sys.stdin.read()
for line in stdin.splitlines():
    print(line)

This is when piped in bash, like so:

command | script.py

This works:

command > /tmp/output
cat /tmp/output | script.py

command | iconv -f cp1258 -t utf-8 | script.py

If it’s an encoding problem, how can reading from sys.stdin twice resolve that problem?

Edit:

Fixed the incorrect print(stdin) => print(line)

Edit2:

Enviroment details:

Windows 10 19042

GNU bash, version 4.4.23(1)-release from git for windows 2.31.1.windows.1

Python 3.9.4

Source: Windows Questions

LEAVE A COMMENT