Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5942777
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T16:18:04+00:00 2026-05-22T16:18:04+00:00

I have successfully managed to pipe a variable to a command which is not

  • 0

I have successfully managed to pipe a variable to a command which is not piped further, in this way:

from subprocess import Popen, PIPE

def exec_command(command, some_input):
    proc = Popen(command, stdout=PIPE, stderr=PIPE, stdin=PIPE)
    (stdout, stderr) = proc.communicate(input=some_input)

… but when trying to pipe it to a further piped command (for example piping to tar, and then further piping to split), it does not seem to work:

from subprocess import Popen, PIPE

def exec_piped_command(command1, command2, some_input):
    proc1 = Popen(command1, stdout=PIPE, stdin=PIPE)
    proc2 = Popen(command1, stdin=proc1.stdout, stdout=PIPE)
    (stdout, stderr) = proc2.communicate(input=some_input)[0]

So, how is the correct way to do this second variant? It seems like the problem with the above code is that the input in the “proc2.communicate()” command does not reach the stdin pipe of proc1? (Not sure though … unfortunatly I’m a bit confused about the subprocess syntax …).

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T16:18:05+00:00Added an answer on May 22, 2026 at 4:18 pm

    One possibility would be to set up the entire command to be executed by a shell (shell=True to among the keyword args of your Popen() call … and only .communicate() with the ends of the whole pipeline (your input going to command1’s stdin, and your stdout/stderr coming from command2’s stdout/stderr).

    A more complicated, but fine grained, approach would be to use the os.fork() along with os.pipe() and os.dup2() and possibly some os.fcntl() calls to set up your own subprocesses, with your own custom plumping, and any of the desired blocking/non-blocking characteristics you need on your file descriptors and then, finally, using your own os.exec* functions in each of these.

    This latter approach, obviously, would be duplicating quite a bit of the code that’s already in the subprocess module. However, it provides you the option of doing some of these steps differently in ways that are not exposed via the subprocess.Popen class.

    The middle road would be to make a sub class that inherits from subprocess.Popen

    Of course it may be preferable to perform some of the parts of this pipeline through Python code and modules (such as tar, gzip, and split operations).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have tried to get this function working in the desired way but not
I have successfully managed to get System.Speech.Synthesis to read English text in arbitrary voices
Have anyone successfully managed to setup a combined Java/C++ project for Eclipse? What I
I have successfully implemented interop beftween Win32 application and managed .Net dll as described
After much fiddling, I've managed to install the right ODBC driver and have successfully
I have successfully connected to an Oracle database (10g) from C# (Visual Studio 2008)
I have successfully upgraded an MFC application which was compiled with an old version
I have successfully managed to bind images stored in Isolated Storage to Image elements
I have in the past successfully managed to copy/edit/paste rewrite rules on an apache
Let's say I have this action in a JSF Managed Bean: public String doSomething()

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.