0

I have an API that has functions (e.g. function1, function2, etc) where each function is placed in a file (file1, file2, etc) and all files are in a directory, dir. The structure is as so:

dir
  -> file1.py (contains function1)
  -> file2.py (contains function2)
  ...

I want to create a new function that returns all functions in every file in dir (ie. I want to create a list [function1, function2, ...]) so that the user can view all functions available. Since more functions/files will be create in dir, I want my new function to automatically walk through each file and create the list, rather than hardcoding a list somewhere.

I've been able to use os.listdir to get the filenames, but how can I get functions inside each of the files from the filenames?

Francis Godinho
  • 195
  • 1
  • 9
  • Does [How do I list all functions for a Python module ignoring functions the module imports?](https://stackoverflow.com/questions/63447803/how-do-i-list-all-functions-for-a-python-module-ignoring-functions-the-module-im) answer your question? – wwii Jul 14 '22 at 21:36
  • [How to list all functions in a module?](https://stackoverflow.com/questions/139180/how-to-list-all-functions-in-a-module)? – wwii Jul 14 '22 at 21:38
  • Thanks, but I think one problem was that I had file paths instead of actual modules like in those questions. I found [import_module](https://docs.python.org/3/library/importlib.html) which helped me. – Francis Godinho Jul 14 '22 at 22:05

1 Answers1

0

import_module allows you to import based on a filename.

Francis Godinho
  • 195
  • 1
  • 9