How to trick an application into thinking its stdout is a terminal, not a pipe

Aha! The script command does what we want… script –return –quiet -c “[executable string]” /dev/null Does the trick! Usage: script [options] [file] Make a typescript of a terminal session. Options: -a, –append append the output -c, –command <command> run command rather than interactive shell -e, –return return exit code of the child process -f, –flush … Read more

How do I write to a Python subprocess’ stdin?

It might be better to use communicate: from subprocess import Popen, PIPE, STDOUT p = Popen([‘myapp’], stdout=PIPE, stdin=PIPE, stderr=PIPE) stdout_data = p.communicate(input=”data_to_write”)[0] “Better”, because of this warning: Use communicate() rather than .stdin.write, .stdout.read or .stderr.read to avoid deadlocks due to any of the other OS pipe buffers filling up and blocking the child process.

How do I pass a string into subprocess.Popen (using the stdin argument)?

Popen.communicate() documentation: Note that if you want to send data to the process’s stdin, you need to create the Popen object with stdin=PIPE. Similarly, to get anything other than None in the result tuple, you need to give stdout=PIPE and/or stderr=PIPE too. Replacing os.popen* pipe = os.popen(cmd, ‘w’, bufsize) # ==> pipe = Popen(cmd, shell=True, … Read more

How do I read a string entered by the user in C?

You should never use gets (or scanf with an unbounded string size) since that opens you up to buffer overflows. Use the fgets with a stdin handle since it allows you to limit the data that will be placed in your buffer. Here’s a little snippet I use for line input from the user: #include … Read more

How do you read from stdin?

You could use the fileinput module: import fileinput for line in fileinput.input(): pass fileinput will loop through all the lines in the input specified as file names given in command-line arguments, or the standard input if no arguments are provided. Note: line will contain a trailing newline; to remove it use line.rstrip()

Using fflush(stdin)

Simple: this is undefined behavior, since fflush is meant to be called on an output stream. This is an excerpt from the C standard: int fflush(FILE *ostream); ostream points to an output stream or an update stream in which the most recent operation was not input, the fflush function causes any unwritten data for that … Read more