Python Forum
how to run a command pipeline and get its result output as a piped file - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: General (https://python-forum.io/forum-1.html)
+--- Forum: Code sharing (https://python-forum.io/forum-5.html)
+--- Thread: how to run a command pipeline and get its result output as a piped file (/thread-37895.html)



how to run a command pipeline and get its result output as a piped file - Skaperen - Aug-04-2022

this code is a minimal example of how to run a command pipeline and get its result output as a pipe file. that file is binary so its read result will be type bytes, not str. you know how to fix that. the output of the example usage/test should be the last up to ten Python files in the current directory/folder.

this was originally written with more code to handle more features intended for POSIX systems and tested on Linux. but my recent thoughts are that if pipes work similarly on Windows it may work there or be close to doing so. do you think you can make it work on Windows? the example commands are POSIX specific and probably need changed on Windows. it should work OK in a command shell on Mac OS and Android.

import sys
def run_pipeline_to_read_output(pipeline,**kwargs):
    """Function to run a command pipeline and return file to read its output as bytes."""
    if not('stdin' in kwargs and kwargs['stdin']): # if no alternate specified...
        kwargs['stdin']=sys.stdin # ...first command gets stdin
    kwargs['stdout']=PIPE # all stdout goes to a pipe
    for cmd in pipeline: # each command...
        kwargs['stdin']=Popen(cmd,**kwargs).stdout # ...is started with output to next stdin
    return kwargs['stdin'] # return read end of last pipe for caller to read bytes from

if __name__=='__main__':
    pipeline=[]
    pipeline.append(['ls','-l'])
    pipeline.append(['egrep',r'\.py$'])
    pipeline.append(['tail'])
    file=run_pipeline_to_read_output(pipeline)
    for line in file:
        print(repr(line))
    file.close()
SHA256 is "52ff1fbbc0b006e5c69073b7d74f4cc33ff08b239b0100955f6fe0ff1d64e372"

note that line 4 shows what could be confused as a function named "not" being called. but, as you know, "not" is a keyword so it won't be interpreted that way. so this code could be used as a test unit of a Python code parser to see if it correctly handles that code.