Search code examples
pythonsubprocessstdinpipelinetemporary-files

How, can I pipe the output from the first process (function) to the next process (function) in python?


In the following program

1) I want to write the output to a file 2) And, also want to access the output file downstream in another process by piping the final output of the file, rather than reading from it.

With the following python code:

global product
product = ""
product_file = open('product.txt', 'w')   

def read_file():   
    file1 = open ('file01.txt', 'r')
    data1 = read.file1().rstrip('\n')
    data1_lines = data1.split('\n)
    for lines in data1_lines:
        columns = lines.split('\t')
        x = int(columns[0])
        y = int(columns[1])
        prod = x*y
        output = open('product.txt', 'a')
        output.write(str(x) + '\t' + str(y) + '\t' + str(prod))
        output.close()
        product += str(x +'\t'+ y +'\t'+ prod +'\n')

def manipulate_file():
    global product;
    for lines in product:
        line = product.split('\t')
        do..something.....

I want to access the final output file from def read_file() to be used in the downstream process (function i.e def mainpulate_file()) rather than opening it again.

I want to use either subprocess.call and/or stdin-out and/or tempfile and finally clear the memory when done.

I read several examples but could not find anything clear to work out the process.

I would appreciate any explanation.


Solution

  • We don't even need global argument.

    product = ""
    product_file = open('product.txt', 'w')   
    
    def read_file():   
        file1 = open ('file01.txt', 'r')
        data1 = read.file1().rstrip('\n')
        data1_lines = data1.split('\n)
        for lines in data1_lines:
            columns = lines.split('\t')
            x = int(columns[0])
            y = int(columns[1])
            prod = x*y
            output = open('product.txt', 'a')
            output.write(str(x) + '\t' + str(y) + '\t' + str(prod))
            output.close()
            product += str(x +'\t'+ y +'\t'+ prod +'\n')
        return manipulate_file(product)
    
    def manipulate_file(product):
        data = StringIO(product)
        data_lines = data.read().rstrip('\n).split('\n')
        for lines in data_lines:
            do..something.....
    

    So:

    • Just simply create a variable that can be updated in the loop.

    • Return this variable with the defined function

    • Read the file with the function it was returned too, but this needs to be read using StringIO since the data is on the console.