473,509 Members | 3,009 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

running a script from another script without knowing it's name

9 New Member
Hi,
I want to make a script that will call the run() func of some other scripts who's name the main script will read from a file.

i.e, the main script will:
1. read the file
2. for each <name> in the file:
3. try to call <name>.run()

i.e, if i have a text file:

one
two

the main script will read the text file and then try to call one.run() and two.run()

I can't use the os module since i need the return value of that function.
Aug 21 '07 #1
4 1933
ilikepython
844 Recognized Expert Contributor
Hi,
I want to make a script that will call the run() func of some other scripts who's name the main script will read from a file.

i.e, the main script will:
1. read the file
2. for each <name> in the file:
3. try to call <name>.run()

i.e, if i have a text file:

one
two

the main script will read the text file and then try to call one.run() and two.run()

I can't use the os module since i need the return value of that function.
Use the __import__ function:
Expand|Select|Wrap|Line Numbers
  1. >>> name = "one"
  2. >>> mod = __import__(name)
  3. <module 'one' from blah blah blah>
  4. >>> mod.run() # call function
  5.  
Aug 21 '07 #2
bartonc
6,596 Recognized Expert Expert
Hi,
I want to make a script that will call the run() func of some other scripts who's name the main script will read from a file.

i.e, the main script will:
1. read the file
2. for each <name> in the file:
3. try to call <name>.run()

i.e, if i have a text file:

one
two

the main script will read the text file and then try to call one.run() and two.run()

I can't use the os module since i need the return value of that function.
In the text files, remember to leave off the '.py' extension.
For every .txt file in a given directory, use:
Expand|Select|Wrap|Line Numbers
  1. import os.path
  2. import sys
  3. import glob
  4. for fullPath in glob.glob(r"aPath\*.txt"):
  5.     thisDir, fName = os.path.split(fullPath)
  6.     if thisDir not in sys.path:
  7.         sys.path.append(thisDir)
  8.     f = open(fName)
  9.     lineList = f.readlines()
  10.     f.close()
  11.     for line in lineList:
  12.         try:
  13.             mod = __import__(line.strip())
  14.             mod.run()
  15.         except ImportError, AttributeError:
  16.             pass  # either no module or no run() function
If you are hard-coding the names, use:
Expand|Select|Wrap|Line Numbers
  1. fNames = ["one.txt", "two.txt"]
  2. for fName in fNames:
  3.     # current working directory; leave out os and sys stuff
  4.     # everything else is the same here
Aug 21 '07 #3
noama
9 New Member
and what do i do if the module i want to import isn't in the same directory as the main script, but it's not in a package (the directory doesn't contain a __init__.py file) either?
Aug 25 '07 #4
bartonc
6,596 Recognized Expert Expert
and what do i do if the module i want to import isn't in the same directory as the main script, but it's not in a package (the directory doesn't contain a __init__.py file) either?
OK. I think that I get the scenario:

For a script that reads a text file from its own (current working) directory, the text file might have:
Expand|Select|Wrap|Line Numbers
  1. # module list
  2. C:\rest\of\fully\quallified\path\Module1.py
  3. C:\rest\of\fully\quallified\path\Module2.py
and this would import the modules listed in it:
Expand|Select|Wrap|Line Numbers
  1. import os.path
  2. import sys
  3.  
  4. modListFile = open("thelistfile.txt")
  5. moduleList = modListFile.readlines()
  6. modListFile .close()
  7.  
  8. for line in moduleList:
  9.     fullPath = os.path.normpath(line.strip())
  10.     thisDir, fName = os.path.split(fullPath)
  11.     modName, ext = os.path.splitext(fName)  # this is new #
  12.     if thisDir not in sys.path:
  13.         sys.path.append(thisDir)
  14.     try:
  15.         mod = __import__(modName)
  16.         mod.run()
  17.     except ImportError, AttributeError:
  18.         pass  # either no module or no run() function
Which you might have been able to figure out from the original (it's not very different).
Aug 25 '07 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

1
5502
by: Avin Patel | last post by:
Hi, I am looking for script to allow multiple files can be uploaded/attached in webform ( mostly cgi/perl or php). But I don't like the multiple input boxes using "<input type="file" size="40"...
4
4106
by: Christian | last post by:
From a not even newbie: Without knowing much about Python (yet) I'm trying to install the CMS Zope via FTP (with the well documented changes to make it work on an Apache server). By birth Zope...
8
3016
by: Paul Cochrane | last post by:
Hi all, I've got an application that I'm writing that autogenerates python code which I then execute with exec(). I know that this is not the best way to run things, and I'm not 100% sure as to...
4
1555
by: ATS | last post by:
BUG/PRODUCTION-DOWN: UserControl running from HTML not working. Someone please help, I have a production down situation. For whatever reason, UserControls are not owkring from HTML/IE. When...
4
8697
by: Charles Law | last post by:
Is there a way to dynamically remove an event handler from an event without knowing the name of the handler? For example, how can ClassB remove the handler without knowing the name, or how many...
17
2747
by: CES | last post by:
All, I was wondering if their is a way of loading an external script fill from within a script?? <script language="javascript" type="text/javascript"> function test(var){ <script...
4
2148
by: petermichaux | last post by:
Hi, I'm hoping for a reason I'm wrong or an alternate solution... I'd like to be able to dynamically include some javascript files. This is like scriptaculous.js library but their solution is...
17
2618
by: Csaba Gabor | last post by:
Is there a way to determine the path to the php executable (as opposed to the script. In other words, I am looking for the path to php.exe or php-win.exe) that is currently running (ie. how was...
51
4083
by: Ojas | last post by:
Hi!, I just out of curiosity want to know how top detect the client side application under which the script is getting run. I mean to ask the how to know whether the script is running under...
0
7344
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7412
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
7069
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7505
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5652
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
3216
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
1570
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
775
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
441
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.