One can redirect input from a file with the < operator.  So if I have a Python script, like:name = input(Enter your name: )print(Hello, name)Then I can put the input in a file like this:BobAnd then run it with this:$ python program.py < input.txtWhen doing this, the output looks like this:What is your name? Hello  BobIs there any way to have the input text appear on the screen as well, so that it looks the same as if you ran the program normally?  For the example above, it would look like this:What is your name? BobHello BobThe reason that I want this is because I am working on a book which has code examples, and I want to automate running the programs and having the output appear in the book.  I don't know if this is possible, but it would make the whole process much easier since I can just write the programs, and setup the input files, and then have the rest happen automatically!Thanks for reading!
Redirect input from file, but show in stdout as well?
shell;io redirection
You could use an expect script to feed a given input file to a given python script:ian.expect:set script [lindex $argv 0]set input  [lindex $argv 1]set inputfh [open $input r]spawn -noecho python $scriptwhile {[gets $inputfh line] != -1} {  expect {    -re (.+) {        send $line\n    }  }}close $inputfhinteractian.py:name = input(Enter your name: )print(Hello, name)input.txt:JeffTo execute any given python script with a given input file, run:expect -f ian.expect ian.py input.txtand you'll get:$ expect -f ian.expect ian.py input.txtEnter your name: Jeff('Hello', 'Jeff')I am not a TCL or Expect hacker, so I would welcome improvements to the script.Example file and script with two inputs:input.txt:Jeff14ian.py:name = input(Enter your name: )print(Hello, name)age = input(Your age? )print(So, you are already  + str(age) +  years old,  + name + !)sample run:$ expect -f ian.expect ian.py input.txtEnter your name: Jeff('Hello', 'Jeff')Your age? 14So, you are already 14 years old, Jeff!