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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T01:10:08+00:00 2026-05-25T01:10:08+00:00

Can someone assist me in thinking my way through this problem. In the code

  • 0

Can someone assist me in thinking my way through this problem. In the code below, I’m taking a list and opening all of the .log and .txt files, as to search them for a particular string. In the inner most for loop there is an if and else statement that determines if a string was found or not. I want to count the number of files that a string was matched in …, and some kind of way pass that to the third(last) for loop and display … (eg. Files Matched: 4). I’m still learning python, so I’m unaware of all the different constructs that would expedite this endeavor. I’m sure this is a straight forward problem, but I have exhausted everything I know to do besides rote trial and error. Thanks!

...

for afile in filelist:
    (head, filename) = os.path.split(afile)
    if afile.endswith(".log") or afile.endswith(".txt"):
        f=ftp.open(afile, 'r')
        for i, line in enumerate(f.readlines()):
            result = regex.search(line)
            if result:
                ln = str(i)
                pathname = os.path.join(afile)
                template = "\nLine: {0}\nFile: {1}\nString Type: {2}\n\n"
                output = template.format(ln, pathname, result.group())
                hold = output
                print output
                ftp.get(afile, 'c:\\Extracted\\' + filename)
                temp.write(output)
                break
        else:
            print "String Not Found in: " + os.path.join(afile)
            temp.write("\nString Not Found: " + os.path.join(afile))

        f.close()
for fnum in filelist:
    print "\nFiles Searched: ", len(filelist)
    print "Files Matched: ", count
    num = len(filelist)

    temp.write("\n\nFiles Searched: " + '%s\n' % (num))
    temp.write("Files Matched: ") # here is where I want to show the number of files matched
    break
  • 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-25T01:10:09+00:00Added an answer on May 25, 2026 at 1:10 am

    How about this:

    count = 0
    for afile in filelist:
        (head, filename) = os.path.split(afile)
        if afile.endswith(".log") or afile.endswith(".txt"):
            f=ftp.open(afile, 'r')
            for i, line in enumerate(f.readlines()):
                result = regex.search(line)
                if result:
                    count += 1
                    ln = str(i)
                    pathname = os.path.join(afile)
                    template = "\nLine: {0}\nFile: {1}\nString Type: {2}\n\n"
                    output = template.format(ln, pathname, result.group())
                    hold = output
                    print output
                    ftp.get(afile, 'c:\\Extracted\\' + filename)
                    temp.write(output)
                    break
            else:
                print "String Not Found in: " + os.path.join(afile)
                temp.write("\nString Not Found: " + os.path.join(afile))
    
            f.close()
    for fnum in filelist:
        print "\nFiles Searched: ", len(filelist)
        print "Files Matched: ", count
        num = len(filelist)
    
        temp.write("\n\nFiles Searched: " + '%s\n' % (num))
        temp.write("Files Matched: "+str(count)) # here is where I want to show the number of files matched
        break
    

    count starts at 0 and increments for every file that there is a match for.

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

Sidebar

Related Questions

Can someone please assist me. I am trying to find a log on an
I have a bewildering problem, hoping someone can assist: I have a model object,
I'm desperately hoping someone can assist me with this error. Admittedly, I'm a novice
Can someone assist me with formatting this JavaScript array correctly? I am obviously missing
I was hoping someone can assist in describing a workaround solution to the following
Can someone explain this result to me. The first test succeeds but the second
This is a total newbie question, but I wonder if someone could assist with
I've been trying to work out this particular problem and though I can easily
Ive been going round and round with this so I am hoping someone can
can someone assist me with fixing the error i'm receiving. $search_key = ''; /*if(isset($_POST)

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.