Python Forum
how to run a command pipeline and get its result output as a piped file
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
how to run a command pipeline and get its result output as a piped file
#1
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.
Gribouillis likes this post
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Feature Extraction and Modeling Pipeline FelixLarry 1 2,091 Sep-07-2022, 09:42 AM
Last Post: Larz60+
  Command output to pdf (linux) Gribouillis 0 5,010 Mar-05-2021, 07:35 PM
Last Post: Gribouillis
  pipeline.py Skaperen 11 9,025 Feb-25-2017, 05:11 AM
Last Post: Skaperen

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020