I have the following problem:
Let's say I have a directory and inside this directory is another directory that contains a hello_world.py file with the following function:
def print_hello_world():
print("Hello World")
Hierarchy: folder1/folder2/hello_world.py
Now I have in folder1 a print_function_names.py file that checks if folder2 exists and after that I iterate over the .py files in folder2 and print the function names. In this test scenario the output is:
print_hello_world
My first approach was to open the file and use a regex expression to find the function name.
folder1/print_function_names.py
with open("folder1/folder2/hello_world.py", "r") as file:
for line in file.readlines():
if re.match("def (.*)\(", line):
function_name = re.match("def (.*)\(", line)
print(function_name.group(1))
Is there maybe a more pythonic or maybe easier way?