How do I use pipe when running python programs in the terminal, and what is it for?

Asked

Viewed 98 times

-1

example : python3 example.py | example2.py I wanted to know what this is for, I realized that somehow they communicate , but I did not understand the usefulness of this property.

  • 2

    This is not something from Python, but from the terminal. Basically, if you have comando1 | comando2, the exit of comando1 becomes the entrance of the comando2: https://stackoverflow.com/q/9834086

  • Look at our canonical UNIX pipe on the terminal there. For me it is on-topic, common tool in the context of programming. :)

1 answer

2


The pipe, with the signal | is actually an operating system feature - provided by the shell (on Linux and Macos may be bash, among others, and on Windows to this day is "cmd").

What it does is actually connect one program to the other through "stdout"- everything the first program would write on the screen is forwarded to the second program as if someone had typed that on the keyboard.

So simple programs, written in the 1970s and 1980s, were created so that a very large eco-system was formed of various programs that can be interconnected with pipe.

The Unix find, grep, cat, echo, cut, tar, gzip commands plus full programming languages created centered on reading and writing in stdin and stdout like awk, Tcl and perl - took great advantage of this, and even today it is possible to find recipes with these and other commands that do literally "magic" :-)

Both today’s "real" application eco-system (2021) and programming languages have changed - even large terminal systems hardly follow the philosophy of being "pluggable directly" by pipe, (see the example of git) . Of course, there’s always some functionality that will look good.

In the case of Python, on a simple level, without configuring anything else, everything you do "print" in a program can serve as input to an "input" in a second program - so this works here:

# dados.py
print("João")
print("Brasileiro")
print("Campinas")

# processa.py
nome = input()
nacionalidade = input()
cidade = input()

print(f"Recebi os dados de {nome}, {nacionalidade}, que mora em {cidade}")

And in my thermial, it can work like this:

gwidion@village ~/tmp62 $ python dados.py|python processa.py                                                                              
Recebi os dados de João, Brasileiro, que mora em Campinas

This communication can go well beyond these simple data - just as the stdout of program1 is connected to program2, by Python it is possible to connect back, the output of program2 to an independent input for program1 - the module subprocess Python allows all this control, and you can create programs specifically to talk to each other.

  • Thanks ! But that wouldn’t be equivalent to typing in the terminal : py processes.py < data.py ?

  • so - in this example yes - but the "data.py" could take this data from anywhere- from a database, from a text file, from a web request - -just like the "processes.py' could do anything with them: zip, write to a database, etc. . E having 3 fixed and distinct fields also doesn’t help - it’s a Ruin example - it’s mostly used for homogeneous data: the second program treats all text that is output from program 1, etc...

Browser other questions tagged

You are not signed in. Login or sign up in order to post.