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 7164387
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T14:04:01+00:00 2026-05-28T14:04:01+00:00

On Linux, I’m using supbprocess.Popen to run an app. The command line of that

  • 0

On Linux, I’m using supbprocess.Popen to run an app. The command line of that app requires a path to an input file. I learned I can pass the path /dev/stdin to the command line, and then use Python’s subproc.stdin.write() to send input to the subprocess.

import subprocess
kw['shell'] = False
kw['executable'] = '/path/to/myapp'
kw['stdin'] = subprocess.PIPE
kw['stdout'] = subprocess.PIPE
kw['stderr'] = subprocess.PIPE
subproc = subprocess.Popen(['','-i','/dev/stdin'],**kw)
inbuff = [u'my lines',u'of text',u'to process',u'go here']
outbuff = []
conditionbuff = []

def processdata(inbuff,outbuff,conditionbuff):
    for i,line in enumerate(inbuff):
        subproc.stdin.write('%s\n'%(line.encode('utf-8').strip()))
        line = subproc.stdout.readline().strip().decode('utf-8')
        if 'condition' in line:
            conditionbuff.append(line)
        else:
            outbuff.append(line)

processdata(inbuff,outbuff,conditionbuff)

There’s also an MS Windows version of this app. Is there an equivalent on MS Windows to using the /dev/stdin or is the a Linux (Posix) specific solution?

  • 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-28T14:04:02+00:00Added an answer on May 28, 2026 at 2:04 pm

    If myapp treats - as a special filename that denotes stdin then:

    from subprocess import PIPE, Popen
    
    p = Popen(['/path/to/myapp', '-i', '-'], stdin=PIPE, stdout=PIPE)
    stdout, _ = p.communicate('\n'.join(inbuff).encode('utf-8'))
    outbuff = stdout.decode('utf-8').splitlines()
    

    If you can’t pass - then you could use a temporary file:

    import os
    import tempfile
    
    with tempfile.NamedTemporaryFile(delete=False) as f:
         f.write('\n'.join(inbuff).encode('utf-8'))
    
    p = Popen(['/path/to/myapp', '-i', f.name], stdout=PIPE)
    outbuff, conditionbuff = [], []
    for line in iter(p.stdout.readline, ''):
        line = line.strip().decode('utf-8')
        if 'condition' in line:
            conditionbuff.append(line)
        else:
            outbuff.append(line)
    p.stdout.close()
    p.wait()
    os.remove(f.name) #XXX add try/finally for proper cleanup
    

    To suppress stderr you could pass open(os.devnull, 'wb') as stderr to Popen.

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

Sidebar

Related Questions

in linux to copy and paste a file we have cp command cp [OPTION]...
On linux, it's possible to create a tun interface using a tun driver which
In Linux, what is the difference between /dev/ttyS0 and /dev/ttys0 ? I know that
Under Linux, my C++ application is using fork() and execv() to launch multiple instances
On linux can we repackage a installed rpm if so how.I remember that the
In Linux(Ubuntu) I am trying to run a tool and it is showing error
Linux gcc 4.4.1 I have this function that passes this second parameter that casts
linux gcc 4.4.1 C99 I am just wondering is there any advantage using the
linux: $HOME/.config windows: %APPDATA% mac os: $HOME/.config It can be set using http://qt-project.org/doc/qt-4.8/qsettings.html#setPath ,
Under Linux, how do I find out which process is using the swap space

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.