stdin

What does sys.stdin read?

What does sys.stdin read? Question: I get how to open files, and then use Python’s pre built in functions with them. But how does sys.stdin work? for something in sys.stdin: some stuff here lines = sys.stdin.readlines() What’s the difference between the above two different uses on sys.stdin? Where is it reading the information from? Is …

Total answers: 6

How to check potentially empty stdin without waiting for input?

How to check potentially empty stdin without waiting for input? Question: I am attempting to read from keyboard input without waiting for input. The purpose is to be used in an "infinite" loop aka while True:. Thus far I’ve been trying to manipulate the readchar library https://pypi.python.org/pypi/readchar/0.6 , but with no luck. While it doesn’t …

Total answers: 1

Interactive input/output using Python

Interactive input/output using Python Question: I have a program that interacts with the user (acts like a shell), and I want to run it using the Python subprocess module interactively. That means, I want the possibility to write to standard input and immediately get the output from standard output. I tried many solutions offered here, …

Total answers: 4

How to open every file in a folder

How to open every file in a folder Question: I have a python script parse.py, which in the script open a file, say file1, and then do something maybe print out the total number of characters. filename = ‘file1’ f = open(filename, ‘r’) content = f.read() print filename, len(content) Right now, I am using stdout …

Total answers: 8

Python 3: How to specify stdin encoding

Python 3: How to specify stdin encoding Question: While porting code from Python 2 to Python 3, I run into this problem when reading UTF-8 text from standard input. In Python 2, this works fine: for line in sys.stdin: … But Python 3 expects ASCII from sys.stdin, and if there are non-ASCII characters in the …

Total answers: 1

How do I write to a Python subprocess' stdin?

How do I write to a Python subprocess' stdin? Question: I’m trying to write a Python script that starts a subprocess, and writes to the subprocess stdin. I’d also like to be able to determine an action to be taken if the subprocess crashes. The process I’m trying to start is a program called nuke …

Total answers: 5

Optional stdin in Python with argparse

Optional stdin in Python with argparse Question: I found the very useful syntax parser.add_argument(‘-i’, ‘–input-file’, type=argparse.FileType(‘r’), default=’-‘) for specifying an input file or using stdinā€”both of which I want in my program. However, the input file is not always required. If I’m not using -i or redirecting input with one of $ someprog | my_python_prog …

Total answers: 3

How do you read from stdin in python from a pipe which has no ending

How do you read from stdin in python from a pipe which has no ending Question: I’ve problem to read from Standard input or pipe in python when the pipe is from a “open” (do not know right name) file. I have as example pipetest.py: import sys import time k = 0 try: for line …

Total answers: 5

How do I check if stdin has some data?

How do I check if stdin has some data? Question: In Python, how do you check if sys.stdin has data or not? I found that os.isatty(0) can not only check if stdin is connected to a TTY device, but also if there is data available. But if someone uses code such as sys.stdin = cStringIO.StringIO(“ddd”) …

Total answers: 7

Read from File, or STDIN

Read from File, or STDIN Question: I’ve written a command line utility that uses getopt for parsing arguments given on the command line. I would also like to have a filename be an optional argument, such as it is in other utilities like grep, cut etc. So, I would like it to have the following …

Total answers: 9