fdutils/files.py
def replace_text_in_file(file_path, replace_this, for_that, case_insensitive=False, is_regex=False, keep_copy=False,
                         number_of_subs=0):
    """ replace a string or regex (if is_regex is set) from a file given in file_path, with another string.

        This is a replacement for sed if needed.

    @param str file_path: path to the file to be changed
    @param str replace_this: string or regex to match and replace
    @param str for_that: string that will replace the match
    @param bool case_insensitive: flag to indicate if case is important
    @param bool is_regex: flag to indicate if replace_this is a regular expression or a plain string
    @param bool keep_copy: flag to keep copy of original file or not. The original file will be timestamped
    @param int number_of_subs: number of times to do the substitution. A zero means replace all
    @rtype: tuple

    """

    if not is_regex:
        replace_this = re.escape(replace_this)

    new_file_path = duplicate_file_with_stamp(file_path) if keep_copy else file_path

    import fileinput

    for current_line in fileinput.input(file_path, inplace=True):
        current_line, num_subs_made = re.subn(replace_this, for_that, current_line,
                                              flags=(re.IGNORECASE if case_insensitive else 0), count=number_of_subs)
        number_of_subs = 0 if not number_of_subs else (number_of_subs - num_subs_made)

    return file_path, new_file_path
 replace a string or regex (if is_regex is set) from a file given in file_path, with another string.

        This is a replacement for sed if needed.

    @param str file_path: path to the file to be changed
    @param str replace_this: string or regex to match and replace
    @param str for_that: string that will replace the match
    @param bool case_insensitive: flag to indicate if case is important
    @param bool is_regex: flag to indicate if replace_this is a regular expression or a plain string
    @param bool keep_copy: flag to keep copy of original file or not. The original file will be timestamped
    @param int number_of_subs: number of times to do the substitution. A zero means replace all
    @rtype: tuple

    
replace a string or regex (if is_regex is set) from a file given in file_path, with another string.
This is a replacement for sed if needed.


replace a string or regex ( if is_regex is set ) from a file given in file_path with another string . This is a replacement for sed if needed .
def replace_text_in_file(file_path, replace_this, for_that, case_insensitive=False, is_regex=False, keep_copy=False,
                         number_of_subs=0):
    if not is_regex:
        replace_this = re.escape(replace_this)
    new_file_path = duplicate_file_with_stamp(file_path) if keep_copy else file_path
    import fileinput
    for current_line in fileinput.input(file_path, inplace=True):
        current_line, num_subs_made = re.subn(replace_this, for_that, current_line,
                                              flags=(re.IGNORECASE if case_insensitive else 0), count=number_of_subs)
        number_of_subs = 0 if not number_of_subs else (number_of_subs - num_subs_made)
    return file_path, new_file_path
def replace_text_in_file ( file_path , replace_this , for_that , case_insensitive = False , is_regex = False , keep_copy = False , number_of_subs = 0 ) : if not is_regex : replace_this = re . escape ( replace_this ) new_file_path = duplicate_file_with_stamp ( file_path ) if keep_copy else file_path import fileinput for current_line in fileinput . input ( file_path , inplace = True ) : current_line , num_subs_made = re . subn ( replace_this , for_that , current_line , flags = ( re . IGNORECASE if case_insensitive else 0 ) , count = number_of_subs ) number_of_subs = 0 if not number_of_subs else ( number_of_subs - num_subs_made ) return file_path , new_file_path
replace a string or regex (if is_regex is set) from a file given in file_path, with another string.
replace a string or regex ( if is_regex is set ) from a file given in file_path with another string .
""" replace a string or regex (if is_regex is set) from a file given in file_path, with another string.

        This is a replacement for sed if needed.

    @param str file_path: path to the file to be changed
    @param str replace_this: string or regex to match and replace
    @param str for_that: string that will replace the match
    @param bool case_insensitive: flag to indicate if case is important
    @param bool is_regex: flag to indicate if replace_this is a regular expression or a plain string
    @param bool keep_copy: flag to keep copy of original file or not. The original file will be timestamped
    @param int number_of_subs: number of times to do the substitution. A zero means replace all
    @rtype: tuple

    """
file_path
replace_this
for_that
case_insensitive
is_regex
keep_copy
number_of_subs