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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T05:08:52+00:00 2026-06-06T05:08:52+00:00

I have a string like ‘apples’. I want to find this string, and I

  • 0

I have a string like ‘apples’. I want to find this string, and I know that it exists in one out of hundreds of files. e.g.

file1
file2
file3
file4
file5
file6
...
file200

All of these files are in the same directory. What is the best way to find which file contains this string using python, knowing that exactly one file contains it.

I have come up with this:

for file in os.listdir(directory):
    f = open(file)
    for line in f:
        if 'apple' in f:
            print "FOUND"
    f.close()

and this:

grep = subprocess.Popen(['grep','-m1','apple',directory+'/file*'],stdout=subprocess.PIPE)
found = grep.communicate()[0]
print found
  • 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-06T05:08:53+00:00Added an answer on June 6, 2026 at 5:08 am

    Given that the files are all in the same directory, we just get a current directory listing.

    import os
    
    for fname in os.listdir('.'):    # change directory as needed
        if os.path.isfile(fname):    # make sure it's a file, not a directory entry
            with open(fname) as f:   # open file
                for line in f:       # process line by line
                    if 'apples' in line:    # search for string
                        print 'found string in file %s' %fname
                        break
    

    This automatically gets the current directory listing, and checks to make sure that any given entry is a file (not a directory).

    It then opens the file and reads it line by line (to avoid problems with memory it doesn’t read it in all at once) and looks for the target string in each line.

    When it finds the target string it prints the name of the file.

    Also, since the files are opened using with they are also automatically closed when we are done (or an exception occurs).

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

Sidebar

Related Questions

I have string like this 0100110011001 I want to convert it to byte array
I have string like fullData1 upto fullData10 in this i need to separate out
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
Lets say I have string like this: String q = foo (one) bla (two)
I have a string like this HelloWorldMyNameIsCarl and I want it to become something
I have a Java String like this: peque\u00f1o. Note that it has an embedded
suppose I have string like this str1ng for test now i want to check
I have string like #ls/?folder_path=home/videos/ how i can find last text from string? this
I have string like this /c SomeText\MoreText Some Text\More Text\Lol SomeText I want to
I have string like 123-123-1234 so I want to convert string into this format

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.