A simpler more pythonic approach to adding in

rh0dium sklass at pointcircle.com
Wed Jan 18 23:38:29 CET 2006


Hi all,

I need a cleaner ( and shorter ) way to to look in my home directory or
any directory for a directory called modules.  This is what I currently
have - but it is really ugly.  Some a few of you experts point me to a
cleaner more pythonic approach?

      mod = 0
        if os.path.exists( os.environ['HOME'] + "/modules"):
            sys.path.insert( 0, os.environ['HOME'] + "/modules")
            mod =1
        else:
            path = os.environ['PWD']
            while path != "/":
                if os.path.exists( path + "/modules"):
                    sys.path.insert( 0, path + "/modules")
                    mod= 1
                    path = "/"
                else:
                    path = os.path.split(path)[0]
        
        if mod:
            from foo import foo




More information about the Python-list mailing list