May-30-2018, 01:18 PM
The following was tested on Python 3.6.5 using Windows 10 in the United States:
NOTE: The follow is NOT Python code but a Windows .bat file
a. RedirectStdin-002.bat
b. RedirectStdin-002.py
Lewis
# Tested using Python 3.6.5 on Windows 10 in the United States # # Reference: https://python-forum.io/Thread-detect-if-sys-stdin-is import sys if sys.stdin.isatty(): print ("Do Nothing - stdin is either 'tty' or 'null device'.") else: print ("Process 'stdin' from 'pipe' or from 'file redirection'.") for i, dataline in enumerate(sys.stdin, start=1): print(i, dataline)The following Windows cmd.exe script was used to test the code (e.g. file abc.bat):
NOTE: The follow is NOT Python code but a Windows .bat file
@echo off echo Testing stdin >ScratchStdOut.txt echo. >>ScratchStdOut.txt echo ###################### >>ScratchStdOut.txt echo Testing 'Keyboard' stdin >>ScratchStdOut.txt python redirectStdin-002.py >>ScratchStdOut.txt echo ###################### >>ScratchStdOut.txt echo Testing 'Null Device' stdin >>ScratchStdOut.txt python redirectStdin-002.py >>ScratchStdOut.txt <nul echo ###################### >>ScratchStdOut.txt rem NOTE: Windows prevents this from running if the redirected 'stdin' file DOES NOT EXIST rem This creates and deletes file 'Scratch00.txt' echo Testing 'Redirected' File stdin >>ScratchStdOut.txt echo Hello >Scratch00.txt echo World >>Scratch00.txt python redirectStdin-002.py >>ScratchStdOut.txt <Scratch00.txt if exist Scratch00.txt del Scratch00.txt echo ###################### >>ScratchStdOut.txt echo Testing 'Pipe' stdin >>ScratchStdOut.txt echo Pipe stdin text - Hello World | python redirectStdin-002.py >>ScratchStdOut.txt echo ###################### >>ScratchStdOut.txt type ScratchStdOut.txt pause exitResults using the .bat file:
Output:Testing stdin
######################
Testing 'Keyboard' stdin
Do Nothing - stdin is either 'tty' or 'null device'.
######################
Testing 'Null Device' stdin
Do Nothing - stdin is either 'tty' or 'null device'.
######################
Testing 'Redirected' File stdin
Process 'stdin' from 'pipe' or from 'file redirection'.
1 Hello
2 World
######################
Testing 'Pipe' stdin
Process 'stdin' from 'pipe' or from 'file redirection'.
1 Pipe stdin text - Hello World
######################
See the .zip file attachment containing:a. RedirectStdin-002.bat
b. RedirectStdin-002.py
Lewis
Attached Files
RedirectStdin-002.zip (Size: 845 bytes / Downloads: 198)
To paraphrase: 'Throw out your dead' code. https://www.youtube.com/watch?v=grbSQ6O6kbs Forward to 1:00