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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T02:21:47+00:00 2026-06-04T02:21:47+00:00

I am trying to get filtered list of all Text and Python file, like

  • 0

I am trying to get filtered list of all Text and Python file, like below

from walkdir import filtered_walk, dir_paths, all_paths, file_paths
vdir=raw_input ("enter director :")

files = file_paths(filtered_walk(vdir, depth=0,included_files=['*.py', '*.txt']))

I want to:

  1. know the total number of files found in given directory

    I have tried options like : Number_of_files= len (files) or for n in files n=n+1 but all are failing as "files" is something called "generator" Object which I searched on python docs but couldn’t make use of it

  2. I also want to find a string e.g. "import sys" in the list of files found in above and store the file names having my search string in new file called "found.txt"

  • 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-04T02:21:48+00:00Added an answer on June 4, 2026 at 2:21 am

    I believe this does what you want, if I misunderstood your specification, please let me know after you give this a test. I’ve hardcoded the directory searchdir, so you’ll have to prompt for it.

    import os
    
    searchdir = r'C:\blabla'
    searchstring = 'import sys'
    
    def found_in_file(fname, searchstring):
        with open(fname) as infp:
            for line in infp:
                if searchstring in line:
                    return True
            return False
    
    with open('found.txt', 'w') as outfp:
        count = 0
        search_count = 0
        for root, dirs, files in os.walk(searchdir):
            for name in files:
                (base, ext) = os.path.splitext(name)
                if ext in ('.txt', '.py'):
                    count += 1
    
                full_name = os.path.join(root, name)
                if found_in_file(full_name, searchstring):
                   outfp.write(full_name + '\n')
                   search_count += 1
    
    print 'total number of files found %d' % count
    print 'number of files with search string %d' % search_count
    

    Using with to open the file will also close the file automatically for you later.

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

Sidebar

Related Questions

I am trying to get list of recent posts from a blog filtered by
Trying to get a list of all possible combinations of 2 arrays passed in
I'm trying to get some information filtered from this kind of page (http://www.ricardo.ch/kaufen/computer-und-netzwerk/apple/ipad/apple-ipad-16gb-3gwifi-neu-case-komplettes-zubehoer/v/an637769334/). I'd
I'm trying get values from a GridView using the following code: foreach (GridViewRow row
I am trying get all html links within a string and replace them using
I am trying get all html links within a string and replace them using
trying to get some data from a plist into the MWPhotoBrowser sample app. Any
Trying to get my css / C# functions to look like this: body {
Trying to get this example working from http://www.munna.shatkotha.com/blog/post/2008/10/26/Light-box-effect-with-WPF.aspx However, I can't seem to get
Trying to get parameters from a PUT request using HttpServlet#doPut: public void doPut(HttpServletRequest request,

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.