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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T15:54:00+00:00 2026-06-01T15:54:00+00:00

I’m trying to walk a directory, retrieve all files within that directory ending with

  • 0

I’m trying to walk a directory, retrieve all files within that directory ending with a certain extension, and write that file to a list. This is list is supposed to be returned after the function is done doing its job.

def grabFiles(source,ext):
    import fnmatch
    import os

    matches = [] #tmp var which is used to return the files matching the search mask.
    for root, dirnames, filenames in os.walk(source):   #for each folder & subfolder do:
        for filename in fnmatch.filter(filenames, ext): #and for each file found like that do:
            matches.append(os.path.join(root, filename))#append the file name&directory to the "matches" variable.
    return(matches) #return the content of matches

Now when I run this using the following:

ext=[".caf", ".lmg", ".chr", ".cdf", ".dds", ".tif", ".mtl", ".cgf", ".cga"]

for filetype in ext:
     files= nPy.grabFiles("D:\\01_SVN\\01_DE\\trunk\\Game",filetype)
     print files

I believe it should be returning me a list with files for each extension in my ext list, right?
Instead it returns a [ ] for each item in the ext list.

If I fire this thing without using a definition, it works just fine :/

import fnmatch
import os
source ="D:\\01_SVN\\01_DE\\trunk\\Game"  
extension = ["*.mtl","*.tif","*.chr"] 
matches = []
for filetype in extension:
    for root, dirnames, filenames in os.walk("%s" % (source)):
        for filename in fnmatch.filter(filenames, "%s" % (filetype)):
            matches.append(os.path.join(root, filename))
print matches  

I’ve been staring at this for over an hour, and Im afraid im practically blind to my own script by now :< Im either incapable of returning a dang list, or Im misunderstanding how returning works – but it should be passing the list from matches to a new var without a problem, no?

  • 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-01T15:54:01+00:00Added an answer on June 1, 2026 at 3:54 pm

    I believe you are matching .ext instead of *.ext. Try adding the star to match the name and it should work.

    def grabFiles(source,ext):
        import fnmatch
        import os
    
        matches = []
        for root, dirnames, filenames in os.walk(source):
            for filename in fnmatch.filter(filenames, '*' + ext):
                matches.append(os.path.join(root, filename))
        return matches
    

    About you programming style:

    • avoid repeating what you are doing in comments. It is useless
    • return is a keyword like print, not a function
    • I would use endswith rather that fnmatch.filter
    • avoid importing in functions
    • instead of source ="D:\\01_SVN\\01_DE\\trunk\\Game" use source=r"D:\01_SVN\01_DE\trunk\Game"
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am trying to render a haml file in a javascript response like so:
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm trying to create an if statement in PHP that prevents a single post
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but

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.