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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T19:52:35+00:00 2026-05-18T19:52:35+00:00

I want to run a command in pythong , using the subprocess module, and

  • 0

I want to run a command in pythong, using the subprocess module, and store the output in a variable. However, I do not want the command’s output to be printed to the terminal.
For this code:

def storels():
   a = subprocess.Popen("ls",shell=True)
storels()

I get the directory listing in the terminal, instead of having it stored in a. I’ve also tried:

 def storels():
       subprocess.Popen("ls > tmp",shell=True)
       a = open("./tmp")
       [Rest of Code]
 storels()

This also prints the output of ls to my terminal. I’ve even tried this command with the somewhat dated os.system method, since running ls > tmp in the terminal doesn’t print ls to the terminal at all, but stores it in tmp. However, the same thing happens.

Edit:

I get the following error after following marcog’s advice, but only when running a more complex command. cdrecord --help. Python spits this out:

Traceback (most recent call last):
  File "./install.py", line 52, in <module>
    burntrack2("hi")
  File "./install.py", line 46, in burntrack2
    a = subprocess.Popen("cdrecord --help",stdout = subprocess.PIPE)
  File "/usr/lib/python2.6/subprocess.py", line 633, in __init__
    errread, errwrite)
  File "/usr/lib/python2.6/subprocess.py", line 1139, in _execute_child
    raise child_exception
OSError: [Errno 2] No such file or directory
  • 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-18T19:52:36+00:00Added an answer on May 18, 2026 at 7:52 pm

    To get the output of ls, use stdout=subprocess.PIPE.

    >>> proc = subprocess.Popen('ls', stdout=subprocess.PIPE)
    >>> output = proc.stdout.read()
    >>> print output
    bar
    baz
    foo
    

    The command cdrecord --help outputs to stderr, so you need to pipe that indstead. You should also break up the command into a list of tokens as I’ve done below, or the alternative is to pass the shell=True argument but this fires up a fully-blown shell which can be dangerous if you don’t control the contents of the command string.

    >>> proc = subprocess.Popen(['cdrecord', '--help'], stderr=subprocess.PIPE)
    >>> output = proc.stderr.read()
    >>> print output
    Usage: wodim [options] track1...trackn
    Options:
        -version    print version information and exit
        dev=target  SCSI target to use as CD/DVD-Recorder
        gracetime=# set the grace time before starting to write to #.
    ...
    

    If you have a command that outputs to both stdout and stderr and you want to merge them, you can do that by piping stderr to stdout and then catching stdout.

    subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
    

    As mentioned by Chris Morgan, you should be using proc.communicate() instead of proc.read().

    >>> proc = subprocess.Popen(['cdrecord', '--help'], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
    >>> out, err = proc.communicate()
    >>> print 'stdout:', out
    stdout: 
    >>> print 'stderr:', err
    stderr:Usage: wodim [options] track1...trackn
    Options:
        -version    print version information and exit
        dev=target  SCSI target to use as CD/DVD-Recorder
        gracetime=# set the grace time before starting to write to #.
    ...
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to run a command in pythong , using the subprocess module, and
I want to run a shell command in Terminal, then clear the console, from
I want to use part of the output of a command run from the
I want to run a tail -f logfile command on a remote machine using
I want to run a command as soon as a certain text appears in
I want to run a web application on php and mysql, using the CakePHP
I'm having a strange issue when using subprocess.Popen.communicate(). For background, I want to execute
Using Python I can test my code in the terminal / command line by
I want to run a weekly batch process in an asp.net page. How can
I want to run javascript/Python/Ruby inside my application. I have an application that creates

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.