Search code examples
pythonunixdiffpiping

Piping a Python script in order for it to be compared to another text file


I had a simple question: I have a python script that generates a list of strings and what I want it to be able to pipe this output so that it can be compared to an existing text file in this form:

python myscript.py | diff test.txt -

The test.txt file in this case is just a text file that contains the words that are on a new line each. How would I got about doing this.


Solution

  • All text displayed by print() or sys.stdout.write() (which is used by print() too) will be piped to other command.

    If you want to get data from other pipe like

    ls | python myscript.py
    
    python myscript.py < data.txt
    

    then use input() or sys.stdin.read()