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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T06:47:57+00:00 2026-06-10T06:47:57+00:00

I am using the subprocess module and check_output() to create a virtual shell in

  • 0

I am using the subprocess module and check_output() to create a virtual shell in my Python script, and it works fine for commands that return a zero exit status, however for ones that don’t it returns an exception without printing the error that would have been displayed in the output on a normal shell.

For instance, I would expect something to work like this:

>>> shell('cat non-existing-file')
cat: non-existing-file: No such file or directory

But instead, this happens:

>>> shell('cat non-existing-file')
CalledProcessError: Command 'cat non-existing-file' returned non-zero exit status 1 (file "/usr/lib/python2.7/subprocess.py", line 544, in check_output)

Even though I could remove the Python exception message using try and except, I still want the cat: non-existing-file: No such file or directory to display to the user.

How would I go about doing this?

shell():

def shell(command):
    output   = subprocess.check_output(command, shell=True)
    finished = output.split('\n')

    for line in finished:
      print line
    return
  • 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-10T06:47:59+00:00Added an answer on June 10, 2026 at 6:47 am

    Something like this perhaps?

    def shell(command):
        try:
            output = subprocess.check_output(command, shell=True, stderr=subprocess.STDOUT)
        except Exception, e:
            output = str(e.output)
        finished = output.split('\n')
        for line in finished:
            print line
        return
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a python based tkinter script which executes some commands using subprocess module.
I am using subprocess module for running commands from another app I know that
From within a python script (main.py), I am using the subprocess module to run
How do I execute the following shell command using the Python subprocess module? echo
I am using the subprocess module like that: ping = subprocess.Popen('fping.exe 192.168.2.3 196.65.58.69', stdout=PIPE)
I create a subprocess using subprocess.Popen() that runs for a long time. It is
I'm currently launching a programme using subprocess.Popen(cmd, shell=TRUE) I'm fairly new to Python, but
I'm facing an awkward problem using the subprocess.call operator in python for a script.
I'm trying to execute a series of commands using Pythons subprocess module, however I
I've been trying to communicate with a program (smbpasswd) using Python's subprocess module without

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.