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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T08:49:41+00:00 2026-06-17T08:49:41+00:00

How can I call an external program which is written in bash script in

  • 0

How can I call an external program which is written in bash script in such a way that the output produced by that script is available in sys.stdout so that I can log the output in a file through python.

For example. I now call them through the following snippet

if os.name == 'nt':
    path = module_dir_run+'/run.bat'
else:
    path = module_dir_run+'/run.sh'
if os.path.isfile(path):
    if (splitargs.arg):
        try:
            call([path, splitargs.arg])
        except:
            pass
    else:
        try:
            call([path])
        except:
            pass
else:
    print "Not found : " + path

when I store the value of sys.stdout = file(filename, "w") it stores whatever which the python outputs, but not what the script outputs.

NOTE: The script which i am trying to run is an interactive script, so after the call has ended, and the control has come back to python, how can i get all what is written in the terminal?
Any suggestions?

  • 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-06-17T08:49:42+00:00Added an answer on June 17, 2026 at 8:49 am

    I always use subprocess.Popen() to run another program from inside a Python script. Example:

    import subprocess
    
    print "Starting process..."
    process = subprocess.Popen(["echo", "a"], shell=False)
    process.wait()
    print "Done"
    

    You can redirect the output of process to another file like this:

    import subprocess
    
    print "Starting process..."
    with open("./out.log", "w") as f:
        process = subprocess.Popen(["echo", "a"], shell=False, stdout=f)
        process.wait()
    print "Done"
    

    Redirecting stderr is also possible through ‘stderr’ parameter.

    When you have the sys.stdout in current script redirected to write to your own file, you can do this to redirect it in your subprocess too:

    import subprocess
    import sys
    
    sys.stdout = open("./out.log", "w")
    print "Starting process..."
    sys.stdout.flush()
    process = subprocess.Popen(["echo", "a"], shell=False, stdout=sys.stdout)
    process.wait()
    print "Done"
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How can I call an external program with a python script and retrieve the
I am writing a program that will call a function from an external library,
When I call an external .exe program in Python, how can I get printf
I want to call a perl script as an external program in a PHP
I understand that I can call ToString().IndexOf(...), but I don't want to create an
(Yes I know I can call Java code from Scala; but that is pointless;
I have written a program a.exe which launches another program I wrote, b.exe ,
When starting the external program (let's call it EX for brevity) from within Matlab
I have an external command line program that I would like to invoke from
I've got a program which I'm maintaining, which was written by the guy I

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.