How to make sure process fully completes before moving on in Python API?

I’m trying to import all files in a directory, save out the level, and then close the editor. When I reopen the project, only a percentage of the assets were actually imported. The Python script is executing the next commands before it finished the previous one.