Changeset 4636


Ignore:
Timestamp:
Jul 24, 2007, 10:46:49 AM (10 years ago)
Author:
nick
Message:

get_all_files_with_extension and get_all_directories_with_name added. They are very useful in finding files and directories with certain names in them

File:
1 edited

Legend:

Unmodified
Added
Removed
  • anuga_core/source/anuga/shallow_water/data_manager.py

    r4612 r4636  
    7777#from shutil import copy
    7878from os.path import exists, basename, join
     79from os import getcwd
    7980
    8081
     
    56935694    return iterate_over
    56945695
     5696def get_all_files_with_extension(look_in_dir='',base_name='',extension='.sww',verbose=False):
     5697    '''
     5698    Finds all the sww files in a "look_in_dir" which contains a "base_name".
     5699   
     5700   
     5701    Returns: a list of strings
     5702       
     5703    Usage:     iterate_over = get_all_swwfiles(dir, name)
     5704    then
     5705               for swwfile in iterate_over:
     5706                   do stuff
     5707                   
     5708    Check "export_grids" and "get_maximum_inundation_data" for examples
     5709    '''
     5710   
     5711    #plus tests the extension
     5712    name, ext = os.path.splitext(base_name)
     5713#    print 'look_in_dir',look_in_dir
     5714
     5715    if ext <>'' and ext <> extension:
     5716        msg = msg='base_name %s must be an file with %s extension!'%(base_name,extension)
     5717        raise IOError, msg
     5718
     5719    if look_in_dir == "":
     5720        look_in_dir = "." # Unix compatibility
     5721#    print 'look_in_dir',look_in_dir, getcwd()
     5722    dir_ls = os.listdir(look_in_dir)
     5723    #print 'dir_ls',dir_ls, base_name
     5724    iterate_over = [x[:-4] for x in dir_ls if name in x and x[-4:] == extension]
     5725    if len(iterate_over) == 0:
     5726        msg = 'No files of the base name %s in %s'\
     5727              %(name, look_in_dir)
     5728        raise IOError, msg
     5729    if verbose: print 'iterate over %s' %(iterate_over)
     5730
     5731    return iterate_over
     5732
     5733def get_all_directories_with_name(look_in_dir='',base_name='',verbose=False):
     5734    '''
     5735    Finds all the sww files in a "look_in_dir" which contains a "base_name".
     5736   
     5737   
     5738    Returns: a list of strings
     5739       
     5740    Usage:     iterate_over = get_all_swwfiles(dir, name)
     5741    then
     5742               for swwfile in iterate_over:
     5743                   do stuff
     5744                   
     5745    Check "export_grids" and "get_maximum_inundation_data" for examples
     5746    '''
     5747   
     5748    #plus tests the extension
     5749
     5750    if look_in_dir == "":
     5751        look_in_dir = "." # Unix compatibility
     5752#    print 'look_in_dir',look_in_dir
     5753    dir_ls = os.listdir(look_in_dir)
     5754#    print 'dir_ls',dir_ls
     5755    iterate_over = [x for x in dir_ls if base_name in x]
     5756    if len(iterate_over) == 0:
     5757        msg = 'No files of the base name %s'\
     5758              %(name)
     5759        raise IOError, msg
     5760    if verbose: print 'iterate over %s' %(iterate_over)
     5761
     5762    return iterate_over
    56955763
    56965764
Note: See TracChangeset for help on using the changeset viewer.