run all scripts in sub-directory as subroutines?

Discussion in 'Python' started by guthrie, Aug 11, 2009.

  1. guthrie

    guthrie Guest

    I want to have a program which will form a list of all *.py scripts in
    a sub-directory, and then call some standard messages on them. So I
    can add a new data source modularly by just dropping a new file into
    the sources directory with the appropriate methods in it.

    For example:

    path = sys.path[0]
    print "Starting Directory:: " + path

    getDir=path + "\Sources" # point to directory of data
    sources
    for name in os.listdir(getDir): # run collection from each source
    src = imp.load_source("data",getDir,open(getDir+"\\"+name, 'rb'))
    src.getData()
    src.doGraph()

    This works fine, but in the sub-modules the sys.path appropriately
    returns the same as from the parent, I want them to know their own
    file names. How?? I can pass it to them, but wondered if there is a
    more self-sufficient way for a module to know from where it was
    invoked.
     
    guthrie, Aug 11, 2009
    #1
    1. Advertising

  2. guthrie

    ryles Guest

    On Aug 11, 5:12 pm, guthrie <> wrote:
    > This works fine, but in the sub-modules the sys.path appropriately
    > returns the same as from the parent, I want them to know their own
    > file names. How?? I can pass it to them, but wondered if there is a
    > more self-sufficient way for a module to know from where it was
    > invoked.


    Instead of sys.path, you may be interested in the __file__ attribute
    will give you a module's filename:

    http://docs.python.org/reference/simple_stmts.html#index-1031

    You could also be more explicit in matching *.py files in your
    subdirectory by using glob:

    http://docs.python.org/library/glob.html
     
    ryles, Aug 13, 2009
    #2
    1. Advertising

  3. guthrie

    Tobiah Guest


    > This works fine, but in the sub-modules the sys.path appropriately
    > returns the same as from the parent, I want them to know their own file
    > names. How?? I can pass it to them, but wondered if there is a more
    > self-sufficient way for a module to know from where it was invoked.


    I like the idea of passing the information to the module.
    Probably by setting a global variable into it.
     
    Tobiah, Nov 13, 2009
    #3
  4. guthrie

    Dave Angel Guest

    Tobiah wrote:
    >> This works fine, but in the sub-modules the sys.path appropriately
    >> returns the same as from the parent, I want them to know their own file
    >> names. How?? I can pass it to them, but wondered if there is a more
    >> self-sufficient way for a module to know from where it was invoked.
    >>

    >
    > I like the idea of passing the information to the module.
    > Probably by setting a global variable into it.
    >
    >
    >
    >

    As ryles pointed out in this thread, over a month ago, that's already
    there. The system loader creates a global variable __file__ with the
    full path to the module. So any module can see exactly where it was
    loaded from.
     
    Dave Angel, Nov 14, 2009
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. TJS
    Replies:
    1
    Views:
    547
    Eliyahu Goldin
    Jun 23, 2004
  2. Ben
    Replies:
    2
    Views:
    955
  3. Lawrence D'Oliveiro

    Death To Sub-Sub-Sub-Directories!

    Lawrence D'Oliveiro, May 5, 2011, in forum: Java
    Replies:
    92
    Views:
    2,168
    Lawrence D'Oliveiro
    May 20, 2011
  4. Fa Sidd
    Replies:
    12
    Views:
    232
    ara howard
    Jan 26, 2008
  5. Replies:
    13
    Views:
    580
    Anno Siegel
    Sep 10, 2007
Loading...

Share This Page