2010-07-20 5 views
0
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 
+1

Quelle partie est une boucle infinie? –

+1

Je suis un peu déconcerté par la ligne '[fflist.append (join (root, f)) pour f dans file_list]' - avez-vous une fonction 'join' définie quelque part? Ou est-ce le résultat d'une ligne comme 'from os.path import join'? –

+0

juste pour signaler: ressemble à nonmatches est écrasé à chaque fois dans la boucle pour fflist. peut être utiliser étendre? nonmatches.extend (get_nonmatches (liste_idée, f)) – dmitko

Répondre

2

Juste quelques commentaires sur ce code, alors que nous vous attendons de nous donner assez d'informations pour résoudre votre problème ..

Il est assez horrible en fonction d'un effet secondaire comme celui-ci

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

quand vous pouvez juste dire

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

Mais qui ressemble à un bug pour moi, voulez-vous dire à itérer sur liste_fichiers encore là? Peut-être vous avez juste besoin

fflist.append(join(root, f)) 

Cette partie semble supprimer la condition de son effet

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 

Pourquoi ne pas écrire comme ça?

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 

Si vous allez juste itérer sur fflist, peut-être vous pouvez transformer walkDir dans un générateur

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 

Maintenant, peut-être vous nous dire ce que la sortie du programme est et pourquoi vous êtes sûr qu'il est une boucle infinie et pas seulement prendre beaucoup de temps à courir. Pour tout ce que nous pouvons dire cette ligne

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

pourrait juste être une très longue prennent le temps d'exécuter

Questions connexes