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.
4 1933
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: -
>>> name = "one"
-
>>> mod = __import__(name)
-
<module 'one' from blah blah blah>
-
>>> mod.run() # call function
-
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: - import os.path
-
import sys
-
import glob
-
for fullPath in glob.glob(r"aPath\*.txt"):
-
thisDir, fName = os.path.split(fullPath)
-
if thisDir not in sys.path:
-
sys.path.append(thisDir)
-
f = open(fName)
-
lineList = f.readlines()
-
f.close()
-
for line in lineList:
-
try:
-
mod = __import__(line.strip())
-
mod.run()
-
except ImportError, AttributeError:
-
pass # either no module or no run() function
If you are hard-coding the names, use: - fNames = ["one.txt", "two.txt"]
-
for fName in fNames:
-
# current working directory; leave out os and sys stuff
-
# everything else is the same here
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?
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: - # module list
-
C:\rest\of\fully\quallified\path\Module1.py
-
C:\rest\of\fully\quallified\path\Module2.py
and this would import the modules listed in it: - import os.path
-
import sys
-
-
modListFile = open("thelistfile.txt")
-
moduleList = modListFile.readlines()
-
modListFile .close()
-
-
for line in moduleList:
-
fullPath = os.path.normpath(line.strip())
-
thisDir, fName = os.path.split(fullPath)
-
modName, ext = os.path.splitext(fName) # this is new #
-
if thisDir not in sys.path:
-
sys.path.append(thisDir)
-
try:
-
mod = __import__(modName)
-
mod.run()
-
except ImportError, AttributeError:
-
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).
Sign in to post your reply or Sign up for a free account.
Similar topics |
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"...
|
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...
|
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...
|
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...
|
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...
| |
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...
|
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...
|
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...
|
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...
|
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,...
|
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...
| |
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...
|
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...
|
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,...
|
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...
|
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 ...
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
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...
| |