Created
July 28, 2011 17:19
-
-
Save b1naryth1ef/1112005 to your computer and use it in GitHub Desktop.
Get all python script from a directory, load them into the environment, and set up a dictionary for usage.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
def loadmodules(): | |
""" | |
Loads all Python scripts in a directory into the enviroment, and a dictionary for easy calling | |
""" | |
#Define our variables | |
home = os.getcwd() | |
filenames = [] | |
mody = {} | |
modules = [] | |
directory = "Directory to load" | |
#Load all the filenames into filenames list | |
for fn in os.listdir(os.path.join(home, directory)): | |
if fn.endswith('.py') and not fn.startswith('_'): | |
filenames.append(os.path.join(home, directory, fn)) | |
#Load each file | |
for filename in filenames: | |
name = os.path.basename(filename)[:-3] | |
try: | |
module = imp.load_source(name, filename) | |
modyname = "name" | |
modyname2 = getattr(module,modyname) | |
print "Loaded:",modyname2 | |
except Exception, e: | |
print >> sys.stderr, "Error loading %s: %s" % (name, e) | |
else: | |
module = getattr(module,modyname2) | |
modules.append(module) | |
return mody | |
modules = loadmodules() | |
modules["ModName"](input) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment