Changeset 47be2684 in flex_extract.git for Source/Python/Mods/tools.py


Ignore:
Timestamp:
Oct 28, 2020, 10:28:24 AM (4 years ago)
Author:
Leopold Haimberger <leopold.haimberger@…>
Branches:
ctbto, dev
Children:
75db9b0
Parents:
697b8d0
Message:

Adaptations to allow for a system installation with separate user and system path. Updated documentation

File:
1 edited

Legend:

Unmodified
Added
Removed
  • Source/Python/Mods/tools.py

    rf61e1df r47be2684  
    249249                        type=none_or_str, default=None,
    250250                        help='The name of the ECMWF server name where the'
    251                         'job script is to be submitted ' 
     251                        'job script is to be submitted '
    252252                        '(e.g. ecgate | cca | ccb)')
    253253
     
    624624    '''Creates a directory.
    625625
    626     If the directory already exists, an information is printed and the creation 
     626    If the directory already exists, an information is printed and the creation
    627627    skipped. The program stops only if there is another problem.
    628628
     
    856856    ----------
    857857    cmd_list : list of str
    858         A list of the components for the command line execution. 
    859         They will be concatenated with blank space for the command 
     858        A list of the components for the command line execution.
     859        They will be concatenated with blank space for the command
    860860        to be submitted, like ['mv', file1, file2] for mv file1 file2.
    861861
     
    910910
    911911    return start_period, end_period
     912
     913
     914def check_for_string_in_file(filepath, search_string):
     915    """
     916    Search for a specific string in a file and return True if
     917    the string was found.
     918
     919    Parameters
     920    ----------
     921    filepath : str
     922        The full file path which is to be examined.
     923
     924    search_string : str
     925        The string which is looked up for in the file.
     926
     927    Return
     928    ------
     929    Boolean :
     930        True : String was found
     931        False : String was not found
     932    """
     933    with open(filepath, 'r') as fio:
     934        for line in fio:
     935            if search_string in line:
     936                return True
     937    return False
     938
     939
     940def overwrite_lines_in_file(filepath, search_string, sub_string):
     941    """
     942    Overwrites lines which contain the given search string with the
     943    substitution string.
     944
     945    Parameters
     946    ----------
     947    search_string : str
     948        The string which is looked up for in the file.
     949
     950    sub_string : str
     951        The string which overwrites the search string.
     952
     953    Return
     954    ------
     955    """
     956    with open(filepath, 'r') as fio:
     957        data = fio.readlines()
     958
     959    with open(filepath, 'w') as fio:
     960        for line in data:
     961            if search_string in line:
     962                fio.write(sub_string)
     963            else:
     964                fio.write(line)
     965
     966    return
     967
Note: See TracChangeset for help on using the changeset viewer.
hosted by ZAMG