Piping 2 python scripts using python

I have 3 python files: 1 for accepting a line, one for changing it, and one for their pipeline. I am new to this concept, so please help me on how to achieve this.

1.py

def main():
    print 'Input Your String : '
    stri = raw_input()
    return stri

main()

      

2.py

import sys

if __name__=='__main__':
    for line in sys.stdin:
        sys.stdout.write(line[::-1])

      

pipe.py

import os

os.system('python 1.py | python 2.py')

      

All I want to achieve is feeding the output from 1.py to 2.py using a different python file. Thank!

Output:

$ python pipe.py
foo

 : gnirtS ruoY tupnI[tecomp@localhost 3264]$ 

      

+3


source to share


2 answers


The problem is that there is no stdout other than your print statement in 1.py, if you call main () with print main()

in 1.py then skip the stdout of the process calling the script, then your code will work with pipe.py

as the next output pipeline from 1.py to 2.py:

from subprocess import PIPE, Popen, STDOUT

p = Popen(["python","1.py"], stdout=PIPE, stderr=STDOUT)
p.wait()
p2 = Popen(["python", "2.py"], stdin=p.stdout, stdout=PIPE, stderr=STDOUT)
p.stdout.close()
out, _ = p2.communicate()
print(out)

      

1.py:

def main():
    return raw_input('Input Your String : ')


if __name__ == "__main__":
    print main()

      



2.py:

import sys

if __name__=='__main__':
    for line in sys.stdin:
        sys.stdout.write(line[::-1])

      

Or using shell = True with check_output:

from subprocess import check_output

out = check_output("python 1.py | python 2.py",shell=True)

      

0


source


I can partially get it to work using a module subprocess

. Python files -

1.py - Here, instead of returning the input, you should print it out.

def main():
    print 'Input Your String : '
    stri = input()
    print stri

main()

      

2.py -

import sys

if __name__=='__main__':
    for line in sys.stdin:
        sys.stdout.write(line[::-1])

      

pipe.py -

import subprocess
import sys
p1 = subprocess.Popen(['python','1.py'] , stdout=subprocess.PIPE, stdin=sys.stdin)
output = p1.communicate()[0]

p2 = subprocess.Popen(['python','2.py'] , stdin=subprocess.PIPE, stdout=sys.stdout)
p2.communicate(output)[0]

      

In Pipe.py, we first call by python 1.py

redirecting its stdin to the main script stdin and redirecting stdout to PIPE, so we can get the output in a variable output

.



We then call by python 2.py

redirecting its output to the main script stdout and redirecting its stdin to PIPE, and then use the function p2.communicate()

to pass in a variable output

as input.

Result -

>> python pipe.py
Blah

 : gnirtS ruoY tupnI
halB

      

Note that it partially works as we do not see the invitation from 1.py

. If this invitation is not required, we can also do -

1.py

def main():
    stri = input()
    print stri

main()

      

Then the result will be -

>> python pipe.py
I inputted something

gnihtemos dettupni I

      

0


source







All Articles