Conflict between sys.stdin and input() - EOFError: EOF when reading a line

2024/4/15 1:07:27

I can't get the following script to work without throwing an EOFError exception:

#!/usr/bin/env python3import json
import sys# usage:
# echo '[{"testname": "testval"}]' | python3 test.pymyjson = json.load(sys.stdin)
print(json.dumps(myjson))answer = input("> ")  # BUG: EOFError: EOF when reading a line

I've read this question which seems to be related: Python STDIN User Input Issue

I think that tells me I need to clear the stdin buffer ? But I'm not sure how because print(sys.stdin.readline()) just outputs a newline and the EOFError is still there.

I also tried using the sys.stdin.flush() method (found in this question: Usage of sys.stdout.flush() method) although I still don't understand what it does because I couldn't find it in the official documentation (3.6), the closest I found was this but it doesn't mention flush:

Please bear in mind that I'm not a programmer nor do I have a CS education or background. I just write scripts to automate parts of my, otherwise non-technical, work. So if you know any good beginner ressource on how stdin/stdout works in the shell with Python please do tell.


By piping input, Python is opening sys.stdin as a FIFO. Otherwise, Python will open sys.stdin to /dev/tty or equivalent.

You can verify this with:

import os,sys,stat
print("isatty():", sys.stdin.isatty())
print("isfifo():", stat.S_ISFIFO(os.fstat(0).st_mode))

Run this twice, once piping in data, once not.

I get:

$ echo "Test" | ./
isatty(): False
isfifo(): True$ ./
isatty(): True
isfifo(): False

So your EOF occurs because the FIFO sys.stdin is opened to is empty.

You can reopen sys.stdin to /dev/tty, however.

j = json.load(sys.stdin)
print(j)sys.stdin = open("/dev/tty")answer = input("> ")

Which would work fine:

$ echo '{"key":"val"}' | python3 ./
{'key': 'val'}
> testing

Related Q&A

Requests - inability to handle two cookies with same name, different domain

I am writing a Python 2.7 script using Requests to automate access to a website that sets two cookies with the same name, but different domains, E.g. Name mycookie, Domain and subdomain…

Python logging from multiple processes

I have a possibly long running program that currently has 4 processes, but could be configured to have more. I have researched logging from multiple processes using pythons logging and am using the So…

Error while fetching Tweets with Tweepy

I have a Python script that fetch tweets. In the script i use the libary Tweepy . I use a valid authentication parameters. After running this script some tweets are stored in my MongoDB and some are r…

Example to throw a BufferError

On reading in the Python 3.3 documentation I noticed the entry about a BufferError exception: "Raised when a buffer related operation cannot be performed.". Now Im wondering in which cases co…

Which algorithm would fit best to solve a word-search game like Boggle with Python

Im coding a game similar to Boggle where the gamer should find words inside a big string made of random letters.For example, there are five arrays with strings inside like this. Five rows, made of six …

Sort using argsort in python

I try to sort an array: import numpy as nparr = [5,3,7,2,6,34,46,344,545,32,5,22] print "unsorted" print arrnp.argsort(arr)print "sorted" print arrBut the output is:unsorted [5, 3, …

pandas DataFrame resample from irregular timeseries index

I want to resample a DataFrame to every five seconds, where the time stamps of the original data are irregular. Apologies if this looks like a duplicate question, but I have issues with the interpolati…

django makemigrations to rename field without user input

I have a model with CharField named oldName. I want to rename the field to newName. When I run python makemigrations, I get a confirmation request "Did you rename model.oldName to model.…

Global Python packages in Sublime Text plugin development

1. SummaryI dont find, how Sublime Text plugins developer can use Sublime Text find global Python packages, not Python packages of Sublime Text directory.Sublime Text use own Python environment, not Py…

Can I use pip install to install a module for another users?

Im wish to install Numpy for the www-data user, but I can not log into this user using login. How can I make www-data make us of the Numpy module?To clarify. Numpy is available for root, and for my de…