Optimizing a script that switches function arguments in code files I made a quick script, that inputs file paths(made for C files), function names and 2 indexes. The script goes over all of the files and switches the indexes of the 2 functions. e.g.:
Running the following code:
python switch_args.py temp_funct 0 1 tmp.c

If tmp.c contains this code:
int my_func(a, b, c, d)

after the execution of the script it would have this code:
int my_func(b, a, c, d)

Code:
https://github.com/martin-varbanov96/toolboxz/blob/master/switch_function_args/switch_args.py:
import argparse
import re

parser = argparse.ArgumentParser()
parser.add_argument(&quot;function_name&quot;, type=str,
                    help=&quot;function name whose arguments will be switched&quot;)
parser.add_argument(&quot;index&quot;, type=int,
                    help=&quot;first argument to be switched&quot;)
parser.add_argument(&quot;destination&quot;, type=int,
                    help=&quot;second argument to be switched&quot;)
parser.add_argument('files', metavar='files', type=str, nargs='+',
                    help='files in which function arguments will be switched')

args = parser.parse_args()
def switch_function_args(func_name, args, index=0, destination=1, ):
    function_call_pattern = &quot;{}[^\)]+&quot;.format(func_name)
    for file in args:
        has_function = False
        file_content = &quot;&quot;
        with open(file, &quot;r&quot;) as f:
            file_content = f.read()
            if(func_name in file_content):
                has_function = True
        if(has_function):
            for current_function_structure in re.findall(function_call_pattern, file_content):
                current_arguments = re.split(&quot;\(&quot;, current_function_structure)[1]
                current_arguments = re.split(&quot;,&quot;, current_arguments)

                # switch arguments
                current_arguments[index], current_arguments[destination] = current_arguments[destination], current_arguments[index]
                fixed_arguments = &quot;,&quot;.join(current_arguments)
                fixed_function_structure = &quot;{}({}&quot;.format(func_name, fixed_arguments)
                old_struct_pattern = current_function_structure.replace(&quot;(&quot;, &quot;\(&quot;)
                file_content = re.sub(old_struct_pattern, fixed_function_structure, file_content)
            with open(file, &quot;w&quot;) as f:
                f.write(file_content)
                
switch_function_args(args.function_name, args.files, index=args.index, destination=args.destination)

Question:
How can this code be optimized for performance?
EDIT:
what is the consistency of the following code and is it a good idea to ignore it?
with open(file, &quot;r&quot;) as f_read:
    # code
    with open(file, &quot;w&quot;) as f_write:
        # code
General
C function arguments are only passed by position and not by name. As such, any attempt to make this modify real code is likely to break callers, and it's possible for that breakage to be silent during compile-time if the exchanged parameters have the same type. That makes this kind of refactoring deeply ill-advised. You're better off using the built-in signature refactoring feature of (pick any grown-up IDE).
Early continuation
Consider rewriting
        if(func_name in file_content):
            has_function = True

as
if func_name not in file_content:
    continue

Note that the parens have been dropped; you don't need a flag variable; and you can de-indent the rest of the loop.
Fragility
Before asking

How can this code be optimized for performance?

you need to make it correct, and it definitely isn't right now for all cases. Functions in C are easily (and fairly often) aliased by pointer, and these aliases will not be modified if the pointer doesn't have the same name as the original function. Following such aliases would only reasonably be done by calling into an existing C parser like Clang/LLVM, which you should probably be doing anyway for robustness.