How can I pass the output from one python script as input to another python script?

For instance:

Script1.py receives an expression from a user and converts it to a postfix expression and returns it or prints it to stdout

script2.py gets the postfix expression from stdin and evaluates it and prints the value

I wanted to do something like this:

python3 script1.py | python3 script2.py

This does not work, could you point me in the right direction, how can I do this?

EDIT -

Here are a few details regarding what doesn't work.

When I execute python3 script1.py | The python3 script2.py terminal asks me for input for the script2.py program, when it should request input for the script1.py program and redirect it as the script2.py input file.

" :", " :" script.

+1
3

, . , :

in_string = input("Enter something")
print(some_function(in_string))

some_function - , ( script).

, "Enter something" , script script. , , script script, script . , script, () . , .

. , . ( ). , input, print , : print("prompt", file=sys.stderr)

, , "tty" (). Python sys.stdin.isatty(). " ", , .

, -! Unix (, cat grep) . , , . , .

0

, nginx script1.py:

import os

os.system("ps aux")

script2.py

import os

os.system("grep nginx")

:

python script1.py | script2.py

,

ps aux | grep nginx
0

os:

fileinput , , , .

, :

import fileinput
with fileinput.input() as f_input:  # This gets the piped data for you
    for line in f_input:
        # do stuff with line of piped data

, , :

$ some_textfile.txt | ./myscript.py

, fileinput :   $./myscript.py some_textfile.txt   $./myscript.py < some_textfile.txt

:

>test.py  # This prints the contents of some_textfile.txt
with open('some_textfile.txt', 'r') as f:
    for line in f:
        print(line)

$ ./test.py | ./myscript.py

, , hashbang #!/usr/bin/env python .

Beazley and Jones Python Cookbook - .

0

Source: https://habr.com/ru/post/1681126/


All Articles