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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T06:02:44+00:00 2026-06-04T06:02:44+00:00

I want to call scripts from a directory (they are executable shell scripts) via

  • 0

I want to call scripts from a directory (they are executable shell scripts) via python.

so far so good:

    for script in sorted(os.listdir(initdir), reverse=reverse):
        if script.endswith('.*~') or script == 'README':
             continue
        if os.access(script, os.X_OK):
            try:
                execute = os.path.abspath(script)
                sp.Popen((execute, 'stop' if reverse else 'start'),
                         stdin=None, stderr=sp.PIPE,
                         stdout=sp.stderr, shell=True).communicate()
            except:
                raise

Now what i Want is: lets say i have a bash script with a start functiont. from which I call

echo “Something”

Now I want to see that echo on sys.stdout and the exit Code. I believe you do this with .communicate() but mine doesn’t work the way i thought it would.

What am I doing wrong?

Any help is much appreciated

  • 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-04T06:02:46+00:00Added an answer on June 4, 2026 at 6:02 am

    Confer http://docs.python.org/library/subprocess.html.

    communicate() returns a tuple (stdoutdata, stderrdata).

    After the subprocess has finished, you can get the return code from the Popen instance:

    Popen.returncode: The child return code, set by poll() and wait() (and indirectly by communicate()).

    Likewise, you can achieve your goals like that:

    sp = subprocess.Popen([executable, arg1, arg2], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
    out, err = sp.communicate()
    if out:
        print "standard output of subprocess:"
        print out
    if err:
        print "standard error of subprocess:"
        print err
    print "returncode of subprocess:"
    print sp.returncode
    

    By the way, I would change the test

        if script.endswith('.*~') or script == 'README':
             continue
    

    into a positive one:

    if not filename.endswith(".sh"):
        continue
    

    It is better to be explicit about what you would like to execute than being explicit about what you do not want to execute.

    Also, you should name your variables in a more general fashion, so script should be filename in the first place. As listdir also lists directories, you can explicitly check for those. Your current try/except block is not proper as long as you do not handle a specific exception. Instead of abspath, you should just concatenate initdir and filename, which is a concept often applied in context of os.listdir(). For security reasons, use shell=True in the constructor of the Popen object only if you are absolutely sure that you require it. Let me suggest the following:

    for filename in sorted(os.listdir(initdir), reverse=reverse):
        if os.path.isdir(filename) or not filename.endswith(".sh"):
             continue
        if os.access(script, os.X_OK):
            exepath = os.path.join(initdir, filename)
            sp = subprocess.Popen(
                (exepath, 'stop' if reverse else 'start'),
                stderr=subprocess.PIPE,
                stdout=subprocess.PIPE)
            out, err = sp.communicate()
            print out, err, sp.returncode
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to call external application from shell script, but this shell script gets
I have a Makefile from which I want to call another external bash script
I want to call shell script on windows environment using java code. I am
Possible Duplicate: How to call another PHP script from a PHP script? I want
So I have .csh script generate.csh I want to call another .csh script from
I have 4 scripts which I want to call one after another through ajax.
If I want to call a script "filtermapq.sh" inside a matlab script. How would
I want to create some kind of AJAX script or call that continuously will
I am trying to import one python script from another. I have a few
In a bash script called through shell in some directory ($PWD), there is a

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.