Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > run all scripts in sub-directory as subroutines?

Reply
Thread Tools

run all scripts in sub-directory as subroutines?

 
 
guthrie
Guest
Posts: n/a
 
      08-11-2009
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+"\\"+nam e, '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.
 
Reply With Quote
 
 
 
 
ryles
Guest
Posts: n/a
 
      08-13-2009
On Aug 11, 5:12*pm, guthrie <(E-Mail Removed)> 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/sim...tml#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
 
Reply With Quote
 
 
 
 
Tobiah
Guest
Posts: n/a
 
      11-13-2009

> 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.


 
Reply With Quote
 
Dave Angel
Guest
Posts: n/a
 
      11-14-2009


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.


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Using Python Scripts with IIS - ASP or Python-based CGI scripts withIIS - which makes more sense? davidj411 Python 0 06-27-2008 04:38 PM
Scripts run using load in "for" loop run out of order Fa Sidd Ruby 12 01-26-2008 12:06 AM
What is required for perl scripts to run correct when launched from rc scripts on HPUX 11? deanjones7@gmail.com Perl Misc 13 09-10-2007 11:58 AM
Stupid question: Making scripts python-scripts Jan Danielsson Python 8 07-22-2005 12:20 AM
Re: Stupid question: Making scripts python-scripts Jp Calderone Python 0 07-21-2005 02:38 PM



Advertisments