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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T17:26:09+00:00 2026-06-07T17:26:09+00:00

Consider the following examples – Python 2.4.3 (#1, Jan 14 2011, 00:20:04) [GCC 4.1.2

  • 0

Consider the following examples –

Python 2.4.3 (#1, Jan 14 2011, 00:20:04)
[GCC 4.1.2 20080704 (Red Hat 4.1.2-48)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import os
>>> os.system("grep -i beatles blur.txt")
Blur's debut album Leisure (1991) incorporated the sounds of Madchester and shoegazing. Following a stylistic change.influenced by English guitar pop groups such as The Kinks, The Beatles and XTC.
0
>>> os.system("grep -i metallica blur.txt")
256
>>>

So, in this case, I don’t want the line having my searched keyword to be printed on the Python shell, I just want the return value i.e. 0 if keyword is present and non-zero if its not. How to achieve that?

  • 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-07T17:26:11+00:00Added an answer on June 7, 2026 at 5:26 pm

    You just need to use -q key of the grep:

    $ python
    Python 2.7.3rc2 (default, Apr  5 2012, 18:58:12) 
    [GCC 4.6.3] on linux2
    Type "help", "copyright", "credits" or "license" for more information.
    >>> import os
    >>> os.system("grep -iq igor /etc/passwd")
    0
    >>> os.system("grep -iq oleg /etc/passwd")
    256
    >>> 
    

    I must note that -q is not portable key of the grep, it will work only with GNU grep (Linux and so on).

    When you want to make it work on all systems, you must use popen/subprocess.Popen and redirections of streams.

    >>> import subprocess
    >>> null = open(os.devnull, "w")
    >>> grep = subprocess.Popen(shlex.split("grep -i oleg /etc/passwd"), stderr = null, stdout = null)
    >>> grep.communicate()
    (None, None)
    >>> print grep.returncode
    1
    >>> grep = subprocess.Popen(shlex.split("grep -i igor /etc/passwd"), stderr = null, stdout = null)
    >>> grep.communicate()
    (None, None)
    >>> print grep.returncode
    0
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Consider the two following Python code examples, which achieves the same but with significant
Consider the following examples of Javascript attributes in HTML elements: <input type=button onclick=somceFunc() />
Consider the following example from APUE book: #include <errno.h> #include <sys/socket.h> int initserver(int type,
Consider the following example: <html> <body> <script type=text/javascript> var str=filename.jpg; var pattOne = new
Consider the following example code: http://code.google.com/apis/maps/documentation/javascript/examples/streetview-simple.html I can do scrollwheel: false on a mapOptions
Searching turns up a simple definition: data hiding. But, consider the following two examples:
Consider following two examples: Example 1: <xs:import namespace=http://example.com/ns schemaLocation=test.xsd/> Example2: <sample:Data Test=true xsi:schemaLocation=http://example.com/test.xsd> How
Consider the following code from java.util.concurrent.locks.AbstractQueuedSynchronizer as one of the many examples of the
Consider the following trivial program adopted from the boost program options examples #include <boost/program_options.hpp>
Consider the following code which is typcial of many ChannelFactory examples: WSHttpBinding myBinding =

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.