简体   繁体   中英

Py Search files in Folder and Subfolders

I'm trying to find a list of files in a directory tree. In essence I provide a text file with all the terms I want to search for (~500) and have it look for them in a directory and subdirectories. However, I'm having problems with - I believe - the steps that the code takes and ends prematurely without searching in all folders.

The code I'm using is ( pattern is the name of a text file):

import os

def locateA(pattern, root):
    file  = open(pattern, 'r')
    for path, dirs, files in os.walk(root):
        for word in files:
            for line in file:
                if line.strip() in word:
                    print os.path.join(path, word), line.strip()

Any ideas on where I'm mistaken?

All or part of the problem may be that you can only iterate through a file once unless you use file.seek() to reset the current position in the file.

Make sure you seek back to the beginning of the file before attempting to loop through it again:

import os

def locateA(pattern, root):
    file  = open(pattern, 'r')
    for path, dirs, files in os.walk(root):
        for word in files:
            file.seek(0)             # this line is new
            for line in file:
                if line.strip() in word:
                    print os.path.join(path, word), line.strip()

for line in file consumes the lines in file the first time and then is empty every time after that.

Try this instead, which fixes that and some other problems:

import os

def locateA(pattern, root):
    patterns = open(pattern, 'r').readlines() # patterns is now an array, no need to reread every time.
    for path, dirs, files in os.walk(root):
        for filename in files:
            for pattern in patterns:
                if pattern.strip() in filename:
                    print os.path.join(path, filename), pattern.strip()

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