8

Given one root directory, I need to import all files from all folders (and folders inside other folders) under the root directory.

I figured this could maybe be done with the help of os.walk(), but no idea how to import the files after "walking" through them.

Is there an easy way to do this?

Skamah One
  • 2,456
  • 6
  • 21
  • 31

1 Answers1

4

To import a module by name (in 3.4), use importlib.load_module, which is a "simplifying wrapper around importlib.__import__". You will see the latter mentioned (as a builtin) in other answers. To be useful, you must, of course, assign each module returned to something -- unless you are importing merely to check syntax or generate .pyc files, as in the compile_all module.

Terry Jan Reedy
  • 18,414
  • 3
  • 40
  • 52