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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T23:29:43+00:00 2026-05-31T23:29:43+00:00

Inside a subprocess call, I want to use shell=True so that it does globbing

  • 0

Inside a subprocess call, I want to use shell=True so that it does globbing on pathnames (code below), however this has the annoying side-effect of making subprocess spawn a child process (which must then be `communicate()d/ poll()ed/ wait()ed/ terminate()d/ kill()ed/ whatevah).

(Yes I am aware the globbing can also be done with fnmatch/glob, but please show me the ‘correct’ use of subprocess on this, i.e. the minimal incantation to both get the stdout and stop the child process.)

This works fine (returns output):

subprocess.check_output(['/usr/bin/wc','-l','[A-Z]*/[A-Z]*.F*'], shell=False)

but this hangs

subprocess.check_output(['/usr/bin/wc','-l','[A-Z]*/[A-Z]*.F*'], shell=True)

(PS: It’s seriously aggravating that you can’t tell subprocess you want some but not all shell functionality e.g. globbing but not spawning. I think there’s a worthy PEP in that, if anyone cares to comment, i.e. pass in a tuple of Boolean, or an or of binary flags)

(PPS: the idiom of whether you pass subprocess...(cmdstring.split() or [...]) is just a trivial idiomatic difference. I say tomato, you say tomay-to. In my case, the motivation is the command is fixed but I may want to call it more than once with a difference filespec.)

  • 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-31T23:29:44+00:00Added an answer on May 31, 2026 at 11:29 pm

    First off — there’s very little point to passing an array to:

    subprocess.check_output(['/usr/bin/wc','-l','A-Z*/A-Z*.F*'], shell=True)
    

    …as this simply runs wc with no arguments, in a shell also passed arguments -l and A-Z*/A-Z*.F* as arguments (to the shell, not to wc). Instead, you want:

    subprocess.check_output('/usr/bin/wc -l A-Z*/A-Z*.F*', shell=True)
    

    Before being corrected, this would hang because wc had no arguments and was reading from stdin. I would suggest ensuring that stdin is passed in closed, rather than passing along your Python program’s stdin (as is the default behavior).

    An easy way to do this, since you have shell=True:

    subprocess.check_output(
        '/usr/bin/wc -l A-Z*/A-Z*.F* </dev/null',
        shell=True)
    

    …alternately:

    p = subprocess.Popen('/usr/bin/wc -l A-Z*/A-Z*.F*', shell=True,
                         stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=None)
    (output, _) = p.communicate(input='')
    

    …which will ensure an empty stdin from Python code rather than relying on the shell.

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

Sidebar

Related Questions

subprocess.call([/home/blah/trunk/blah/run.sh, /tmp/ad_xml, /tmp/video_xml]) I do this. However, inside my run.sh, I have relative paths.
You can use subprocess.call(/usr/bin/python) to open Python shell within a piece of Python code.
inside of my Zend_Db_Table_Rowset Object i found this: [_primary:protected] ... does anybody if theres
I'm using python 2.6.4 and discovered that I can't use gzip with subprocess the
Inside a function I have created, I have this loop that checks if the
Inside Magento templating code I want to get the width and height of the
In a python script I call a bash script as follows: subprocess.Popen([./scan.sh, dir]) Inside
Inside a templated class, I found the expression, *this = NULL What does such
I have a subprocess function (called parseContents) that gets called using the following code:
Inside my code I'm generating hashes of URLs, (which are practically of unbounded length).

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.