I’ve got a Python program that reads from sys.stdin, so I can call it with ./foo.py < bar.png. How do I test this code from within another Python module? That is, how do I set stdin to point to the contents of a file while running the test script? I don’t want to do something like ./test.py < test.png. I don’t think I can use fileinput, because the input is binary, and I only want to handle a single file. The file is opened using Image.open(sys.stdin) from PIL.
I’ve got a Python program that reads from sys.stdin , so I can call
Share
You should generalise your script so that it can be invoked from the test script, in addition to being used as a standalone program. Here’s an example script that does this:
If this is called with a filename, the contents of that file will be displayed. Otherwise, input read from stdin will be displayed. (Being able to pass a filename on the command line might be a useful improvement for your
foo.pyscript.)In the test script you can now invoke the function in
foo.pywith a file, for example: