Login | Register For Free | Help
Search for: (Advanced)

Mailing List Archive: Python: Python

run all scripts in sub-directory as subroutines?

 

 

Python python RSS feed   Index | Next | Previous | View Threaded


guthrie at mum

Aug 11, 2009, 2:12 PM

Post #1 of 4 (260 views)
Permalink
run all scripts in sub-directory as subroutines?

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.
--
http://mail.python.org/mailman/listinfo/python-list


rylesny at gmail

Aug 12, 2009, 6:25 PM

Post #2 of 4 (237 views)
Permalink
Re: run all scripts in sub-directory as subroutines? [In reply to]

On Aug 11, 5:12 pm, guthrie <guth...@mum.edu> 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
--
http://mail.python.org/mailman/listinfo/python-list


toby at rcsreg

Nov 13, 2009, 12:28 PM

Post #3 of 4 (183 views)
Permalink
Re: run all scripts in sub-directory as subroutines? [In reply to]

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


--
http://mail.python.org/mailman/listinfo/python-list


davea at ieee

Nov 14, 2009, 7:36 AM

Post #4 of 4 (173 views)
Permalink
Re: run all scripts in sub-directory as subroutines? [In reply to]

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.


--
http://mail.python.org/mailman/listinfo/python-list

Python python RSS feed   Index | Next | Previous | View Threaded
 
 


Interested in having your list archived? Contact Gossamer Threads
 
  Web Applications & Managed Hosting Powered by Gossamer Threads Inc.