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

  • Home
  • SEARCH
  • 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 8823189
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T06:17:27+00:00 2026-06-14T06:17:27+00:00

I want to call a process and output both its stdd and stout to

  • 0

I want to call a process and output both its stdd and stout to a string for inspection. This code triggers the Unexpected error block.

try:
    proc = subprocess.Popen('ls -ddd 1>&2', stdout=subprocess.PIPE,  stderr=subprocess.PIPE,)
    stdout,stderr = proc.communicate()
    if len(stderr)>1:
        actualResult =stderr
    else:
        actualResult =stdout
    print actualResult
except:
    print "Unexpected error"

Ive based it on http://www.oreillynet.com/onlamp/blog/2007/08/pymotw_subprocess_1.html but am obviously missing something. Is it possible to do this inside a try block?

  • 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-14T06:17:28+00:00Added an answer on June 14, 2026 at 6:17 am

    You should never use a generic Except clause, as this will catch any exception and prevent you from fixing the script (Each and any exception is caught, so how do you know which one occured?).

    Here, if you remove the Except block, you’re faced with a OSError: [Errno 2] No such file or directory. This means that subprocess.Popen hasn’t found the executable you asked for on your path.

    Which is happening because you didn’t pass shell = true to your Popen call.
    Not passing shell = True which means that subprocess.Popen is looking for an executable called "ls -ddd 1>&2", which is equivalent to literally writing "ls -ddd 1>&2" at your prompt and would result in a “command not found!”
    (unless you happened to have an executable file with spaces and ampersands in the filename!)

    This is of course not what you want, what you want is to call the ls command with the argument -ddd and redirecting 1>&2.


    Long story short, add shell = True to your call.

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

Sidebar

Related Questions

i want to call the command prompt command using Process.Start and then using StandardOutput
I want to call a function in a remote process of an injected DLL
I want to call and template and save the output to a variable. I
I want to call the netsh command from .net, I am using the Process
I am currently using the below code to run a process and print its
Say I want to test this module: import osutils def check_ip6(xml): ib_output = osutils.call('iconfig
I wrote code for concurrent server. I want to see output in terminal for
I want to invoke a script, piping the contents of a string to its
I want a Python script to grep for a particular process, extract its PID,
I want to call a webservice using google closures, via jsonp since i am

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.