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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T13:41:00+00:00 2026-05-26T13:41:00+00:00

I am trying to use the grep command in a python script using the

  • 0

I am trying to use the grep command in a python script using the subprocess module.

Here’s what I have:

userid = 'foo12'
p = subprocess.Popen(['grep', "%s *.log"%userid], stdout=subprocess.PIPE)

And it returns nothing.
I am not entirely sure what I am doing wrong so can someone please explain. The current method that I am using that works is by adding the shell=true which makes it output the correct output but as the help pages have pointed out it is unsafe. I need help trying to make this work so that my script isn’t unsafe.

  • 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-26T13:41:01+00:00Added an answer on May 26, 2026 at 1:41 pm

    I think you’re running up against two problems:

    1. This call:

      p = subprocess.Popen(['grep', "%s *.log"%userid]...
      

      will not work as expected without shell=True because the list of arguments are being passed directly to os.execvp, which requires each item to be a single string representing an argument. You’ve squished two separate arguments together into a single string (in other words, grep is interpreting “foo12 *.log” as the pattern to search, and not pattern+file list).

      You can fix this by saying:

      p = subprocess.Popen(['grep', userid, '*.log']...)
      
    2. The second issue is that, again without shell=True, execvp doesn’t know what you mean by *.log and passes it directly along to grep, without going through the shell’s wildcard expansion mechanism. If you don’t want to use shell=True, you can instead do something like:

      import glob
      args = ['grep', userid]
      args.extend(glob.glob('*.log')
      p = subprocess.Popen(args, ...)
      
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to use grep with -v for invert-match along with -e for regular
I'm trying to use ack-grep as a replacement for grep + find in Emacs
I'm trying to kill a process (specifically iChat). On the command line, I use
I'm trying to write a helper function to use in a bash script to
I have a C++ library and a C++ application trying to use functions and
I am trying to use ack-is-better-than-grep (ack) with regular expressions to find lines and
Consider the following command line output using grep: [gjempty@gjempty]$ find . -name *.php |
I am trying to use grep to match lines that contain two different strings.
When I use the command: find . | xargs grep '...' I get the
I'm trying to create a CLI command to have TFS check out all files

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.