Maybe it is reasonable to do a `cwd_dirs = os.listdir(os.getcwd())` at the beginning of this function since it is used multiple times?