I have a problem that only appears on Windows 10 when I run a python script from the console (or a ProcessBuilder in Java) with an stdin input.
When I run this code on Ubuntu :
import sys
print(sys.stdin.read())
With this command :
python3 Execute.py < json.txt
I obtain on my console the content of json.txt : "Hello"
When I run this same command (without "<") with the same code on Windows the process reacts like an infinite loop. (What is problematic for my development environment)
My python version is 3.8.10 in both environments (ubuntu 20.04 WSL1 and Windows 10)
What am I doing wrong?
Thanks
2
Answers
Here’s my simple Windows command line parser. Takes parameters supplied in command line itself (
sys.argv
) as well as input redirected from a file (sys.stdin
).Output:
python cliparser.py abc 222 "c d" < cliparser.py
In your case, you might want to explore the
fileinput
standard library, which works by reading the stdin (or a file, if supplied) line by line:Then you can invoke your script either ways: