Skip to content

Instantly share code, notes, and snippets.

@MojtabaMonfared
Created July 10, 2016 13:56
Show Gist options
  • Select an option

  • Save MojtabaMonfared/250740ada1d5ef425471abce49e15a81 to your computer and use it in GitHub Desktop.

Select an option

Save MojtabaMonfared/250740ada1d5ef425471abce49e15a81 to your computer and use it in GitHub Desktop.
import a Directory to one .py file
import os
import re
import sys
#---------------------------------------------------------------------------------------------------
# Interface
#---------------------------------------------------------------------------------------------------
def do(path, env):
"""
importdir.do("example_dir", globals())
"""
__do(path, env)
# File name of a module:
__module_file_regexp = "(.+)\.py(c?)$"
def __get_module_names_in_dir(path):
""" Returns a set of all module names residing directly in directory "path".
"""
result = set()
# Looks for all python files in the directory (not recursively) and add their name to result:
for entry in os.listdir(path):
if os.path.isfile(os.path.join(path, entry)):
regexp_result = re.search(__module_file_regexp, entry)
if regexp_result: # is a module file name
result.add(regexp_result.groups()[0])
return result
def __do(path, env):
""" Implements do().
"""
sys.path.append(path) # adds provided directory to list we can import from
for module_name in sorted(__get_module_names_in_dir(path)): # for each found module...
env[module_name] = __import__(module_name) # ... import
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment