开发者

Why does this keep going around in a never ending loop?

def get_houseid_list():
    """Returns a list of all house ids from db"""
    print 'Building list of all HouseIDs...'
    houseid_list = []
    houseids = session.query(Episode.HouseID).all()
    for i in houseids:
        houseid_list.append(i[0])
    return houseid_list


def walkDir(top, ignore=[]):
    """Returns a complete list of files from a directory, recursing through subfolders"""
    print 'Building list of files...'
    fflist = []
    for root, dirs, files in os.walk(top):
        dirs[:] = [ dn for dn in dirs if dn开发者_开发技巧 not in ignore ]
        file_list =  [name for name in files if name[0] != '.']
        if len(file_list):
            for f in file_list:
                try:
                    houseid_parse(f)
                    print 'adding...', f
                    [fflist.append(join(root, f)) for f in file_list]
                except HouseIdException:
                    print 'skipping...', f
            print 'Found', len(file_list), 'files in', root
    return fflist


def get_nonmatches(houseid_list, finallist):
    print 'Comparing files to HouseIDs...'
    nonmatches = []
    for id in houseid_list:
        print 'Searching for files to match', id
        for f in finallist:
            if re.search(id, f, re.IGNORECASE):
                nonmatches.append(f)
    return nonmatches


def writeCSV(nonmatch):
    print 'Writing nonmatches to CSV...'
    csv.write('%s' % nonmatch)


if __name__ == "__main__":

    houseid_list = get_houseid_list()
    print len(houseid_list), 'HouseIDs found'
    wdirs = ['/Volumes/Assets/Projects']
    finallist = []
    for d in wdirs:
        fflist = walkDir(d)
    for f in fflist:
        nonmatches = get_nonmatches(houseid_list,f)
    print 'nonmatches', nonmatches


Just some comments on this code, while we wait for you to give us enough information to solve your problem..

It's pretty horrible depending on a side effect like this

[fflist.append(join(root, f)) for f in file_list]

when you can just say

fflist.extend(join(root, f) for f in file_list)

But that looks like a bug to me, do you mean to iterate over file_list again there? Perhaps you just need

fflist.append(join(root, f))

This part seems to remove the condition from it's effect

if len(file_list):
    for f in file_list:
        try:
            houseid_parse(f)
            print 'adding...', f
            [fflist.append(join(root, f)) for f in file_list]
        except HouseIdException:
            print 'skipping...', f
    print 'Found', len(file_list), 'files in', root

Why not write it like this?

for f in file_list:
    try:
        houseid_parse(f)
        print 'adding...', f
        fflist.append(join(root, f))
    except HouseIdException:
        print 'skipping...', f
if file_list:
    print 'Found', len(file_list), 'files in', root

If you are just going to iterate over fflist, maybe you can turn walkDir into a generator

def walkDir(top, ignore=[]):
    """Returns a generator for a complete list of files from a directory,
       recursing through subfolders"""
    for root, dirs, files in os.walk(top):
        dirs[:] = [ dn for dn in dirs if dn not in ignore ]
        file_list =  [name for name in files if name[0] != '.']
        for f in file_list:
            try:
                houseid_parse(f)
                print 'yielding...', f
                yield join(root, f)
            except HouseIdException:
                print 'skipping...', f
        if file_list:
            print 'Found', len(file_list), 'files in', root

Now perhaps you tell us what the output of the program is and why you are sure it's an infinite loop and not just taking a long time to run. For all we can tell this line

houseids = session.query(Episode.HouseID).all()

could just be taking a very long time to execute

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜