简体   繁体   中英

Python - fnmatch function that lists files in a directory keeps previous contents

I'm trying to get all of the files in a certain directory (and its sub-directories), that are of a specific format.

I found a code that could help me here , that goes as follows:

from fnmatch import fnmatch
import os, os.path

def print_fnmatches(pattern, dir, files):
    for filename in files:
    if fnmatch(filename, pattern):
        print os.path.join(dir, filename)

os.path.walk('/', print_fnmatches, '*.mp3')

I changed it up a bit to fit my needs. I created a new module, and these are its contents:

from fnmatch import fnmatch
import os.path

filestotag = []

def listoffilestotag(path):
    os.path.walk(path, fnmatches, '*.txt')
    return filestotag

def fnmatches(pattern, direc, files): 
    for filename in files:
        if fnmatch(filename, pattern):
            filestotag.append(os.path.join(direc, filename)) 

From a different module, I could call listoffilestotag() and it works fine.

However, when I call it for a second time, it seems that 'filestotag' keeps its previous contents. Why? How could I fix this? Please note that I don't entirely understand the implementation I wrote...

Thank you!

In your code you're updating a global variable, so each call to that function is actually updating the same list again and agian. Better pass a local list to fnmatches :

from fnmatch import fnmatch
from functools import partial
import os.path

def listoffilestotag(path):
    filestotag = []
    part = partial(fnmatches, filestotag)
    os.path.walk(path, part, '*.txt')
    return filestotag

def fnmatches(lis, pattern, direc, files):
    for filename in files:
        if fnmatch(filename, pattern):
            lis.append(os.path.join(direc, filename))

filestotag is a global variable; you could initialize it in listoffilestotag before calling os.path.walk .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM